On Mon, Oct 26, 2009 at 5:26 PM, Cs Webgrl
<[email protected]> wrote:
> 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
--
Greg Donald
http://destiney.com/
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---