Sunday, May 26, 2013

respond_to? and global methods in Ruby

Lets have an method defined outside of any class:
def my_method 
    "my Method is executed" 
end 


Checking its persistence with respond_to? seems wrong:
respond_to? :my_method => false

The solution to that issue is that the syntax of respond_to? for global methods is different (and absolutely no logical to me). Global methods are presented as private methods of Object class. So the correct verification is:

Object.respond_to?( :my_method, true )




No comments:

Post a Comment