Talks
Speakers
Events
Topics
Sign in
Home
Talks
Speakers
Events
Topics
Leaderboard
Use
Analytics
Sign in
Suggest modification to this talk
Title
Description
Messenger: The (Complete) Story of Method Lookup by Jay McGavren You call a method on an object, and it invokes the instance method defined on the class. Simple. Except when the method isn't on the class itself, because it's inherited from a superclass. Or a singleton class, mixin, or refinement. Actually, this is kind of complicated! In this talk, we'll take an inside look at Ruby method lookup. We'll start with the basics, like inherited methods, and work our way up to the cutting-edge stuff, like refinements and prepending mixins. You'll leave with a clear understanding of how it all works, and maybe with some better ideas for structuring your code! Help us caption & translate this video! http://amara.org/v/H1fg/
Date
Summarized using AI?
If this talk's summary was generated by AI, please check this box. A "Summarized using AI" badge will be displayed in the summary tab to indicate that the summary was generated using AI.
Show "Summarized using AI" badge on summary page
Summary
Markdown supported
In the talk titled 'Messenger: The (Complete) Story of Method Lookup' presented by Jay McGavren at RubyConf 2015, the complexities of Ruby's method lookup process are explored. The discussion is divided into two main parts: methods on classes and methods within modules, emphasizing how Ruby looks for methods during the execution of code. ### Key Points Discussed: - **Method Lookup Basics:** - Understanding where Ruby searches for methods when they are called on an object, starting with the object's singleton class, then proceeding to the class, and finally the superclass if necessary. - The `ancestors` method is used to observe the order in which Ruby will look for methods and how the inherited method lookup process works. - **Singleton Methods:** - Singleton methods defined specifically on a single object can be used for testing purposes, replacing functionality temporarily to create predictable outputs in tests. - **Method Overriding:** - If a method is defined in both a subclass and its superclass, the subclass's method will override the superclass's method. Use of the `super` keyword allows for invoking the overridden method from the superclass. - **Class Methods and Top-Level Methods:** - Class methods behave as singleton methods on class objects, and top-level methods are private instance methods defined on `Object`, accessible from any class. - **Modules as Method Containers:** - Modules can be mixed into classes, and the method lookup works similarly to inheritance, either using `include` or `prepend` to influence the method resolution order. - **Refinements:** - Introduced in Ruby 2.0, refinements provide a way to safely alter methods for specific contexts without affecting global behavior, as demonstrated by changing the `capitalize` method in a localized scope. ### Conclusion and Takeaways: - Ruby's method lookup process can be intricate but follows a systematic approach, starting with the singleton class of an object, proceeding through the class hierarchy, and account for methods defined in modules. - Understanding method overriding, inclusion, and the use of refinements significantly improves code organization and allows for customizable behavior without widespread impacts on existing classes. - Utilizing the `ancestors` method can aid developers in understanding how Ruby resolves method calls, which is crucial for effective coding practices in Ruby.
Suggest modifications
Cancel