How is it that Ruby allows a class access methods outside of the class implicitly?
Example:
class Candy def land homer end end def homer puts 'Hello' end Candy.new.land #Outputs Hello
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The definition of the ‘homer’ method is adding the method to the Object class. It is not defining a free function.
Class Candy implicitly inherits from Object, and so has access to the methods in Object. When you call ‘homer’ in the ‘land’ method, the method resolution can’t find a definition in the current class, goes to the super class, finds the method you have added to Object, and calls it.