I'm still trying to learn the in's and out's of Ruby and Rails. I've been testing my code a million different ways and have read a number of books and web sites, but something is still not clicking for me. I want to be able to call the protected methods
Ruby's object system is a message passing system. If you have an object foo, and you do:
foo.some_method
foo doesn't perform a method call on itself. foo just looks for somewhere to send the "some_method" message, somewhere that returns true for respond_to?
foo.respond_to?(:some_message)
In knowing how this works, you can send the message yourself, ignoring the private or protected nature of the receiver along the way.
Here's an example:
#!/usr/bin/env ruby
class Foo private def hidden puts "I am hidden" end end
f = Foo.new
begin f.hidden rescue puts "Can't run hidden: #{ $! }" end
f.send( :hidden )
./private.rb
Can't run hidden: private method `hidden' called for #<Foo:0xb7c2f92c> I am hidden