00:00:20.359
Hello, everyone. My name is Jeremy McAnally, and I've written a lot of open-source software. This talk is a little bit about that.
00:00:31.000
I didn't get a sound feed, so you all won't enjoy the experience quite like I do, but there is one in the back of the room on your right side. Please let me know if it's working.
00:00:38.239
I’ve recently finished a book called 'Ruby in Practice.' While you might have seen 'The Mr. Book' floating around the internet, I can almost guarantee none of you have seen my Ruby text because it isn’t published yet, but it’s complete and in production. It should be available soon, and I’ll give away two copies to attendees. I have no objective standard for choosing who gets it, so I'll figure something out.
00:01:02.000
I work at ENTP with Rick Olsen and Justin Palmer, and we've organized a Ruby Hoedown. I’ve spoken at a few other conferences as well. Today, we'll be discussing a talk titled 'Deep Ruby'. It will be more of a meta-level talk, diving a bit deeper into Ruby’s object model, aiming to provide clarity on the subject.
00:01:53.680
To illustrate some concepts, I'm going to use a reference to 'The Chronicles of Narnia.' For instance, there's a conversation between Aslan and Edmund about deep magic. Just like that conversation, this talk aims to answer many questions about Ruby's object model.
00:02:34.280
If you have any questions, please feel free to raise your hand. If I miss your question, don't hesitate to yell at me or wave your arms, and I’ll do my best to address it. I wanted to give this talk because many developers—myself included—often struggle with fully understanding Ruby's object model. Sometimes, we don’t exploit Ruby's capabilities to their fullest.
00:02:57.680
To help explain more about the Ruby object model, I won't bombard you with complex object graphs, but I want to discuss what makes Ruby unique. It's essential to think about Ruby’s characteristics when we talk about its objects.
00:03:25.760
In Ruby, everything is an object, from classes to numbers. This is in contrast to other programming languages where this definition of objects can become confusing. In Ruby, unlike in some other languages, objects are defined by their behavior rather than being constrained by class definitions.
00:04:06.680
When discussing objects in programming, it's common to view them like bricks. In C, objects are pretty rigid and solid, whereas in Ruby, they resemble Lego bricks. You can manipulate Lego bricks to create various structures, showing a more flexible mindset towards working with objects.
00:05:00.000
In Ruby, the objects are defined by their behavior rather than fixed class properties. At runtime, you can alter an object's behavior dynamically, allowing for varied and flexible interactions. The difference here is profound, as C does not permit many alterations without changing the underlying structure.
00:05:45.680
This talk will focus on behavior, as it creates the protocols for interaction. In statically typed languages, classes dictate what an object can do. In contrast, in Ruby, if an object has a method, then it can interact with other objects as needed.
00:07:03.960
For example, consider how Ruby leverages the 'Enumerable' module. If an object responds to the 'each' method and takes a block, it can seamlessly integrate with the 'Enumerable' functionalities regardless of whether it's an array, hash, or set.
00:07:19.200
As you build these protocols and conform to them in Ruby, you establish interactive behaviors among various objects. This leads to creating cohesive systems easily, simulating a Lego-like building experience.
00:08:00.640
Ruby's object model favors composition over inheritance—We typically compose classes using modules rather than constructing them purely through inheritance. This approach allows for greater flexibility and encourages building robust systems.
00:08:45.840
To illustrate this point further, I often find it beneficial to explore behaviors for classes and methods using open classes and module composition. This makes for exceptionally powerful mechanics that, when harnessed correctly, can be highly effective.
00:09:40.000
You can simply open a Ruby class and add methods directly. This practice is common in libraries like Active Support, which offers many helpful methods stemming from this flexibility.
00:10:20.280
However, you should document these changes clearly to prevent unexpected behavior. Ruby's design allows developers to alter specific object behaviors, harnessing the power of dynamic programming. David Black's works provide fantastic insight into this flexibility and how it plays out within Ruby.
00:11:03.360
Ruby's dynamic nature enables you to change class behaviors and methods at runtime efficiently. It facilitates a programming style where you can manipulate methods dynamically, sculpting your code as needed according to specific nuances during execution.
00:12:00.920
Methods like class eval and instance eval give developers the flexibility to change behaviors but also require caution. Utilizing these methods too frequently can result in complex code that reduces readability and maintainability.
00:12:51.680
While instance eval allows you to alter a single object's behavior, I commonly advocate for Singleton classes. Through employing Singleton classes, your code can achieve better performance by keeping a consistent structure rather than engaging in unnecessary alterations.
00:13:33.040
You can leverage instance eval where necessary, but it's prudent to limit its use to scenarios where its benefits outweigh the potential downsides. Simple behaviors—like deriving objects from the existing class structures—can help maintain optimal execution speeds inside Ruby.
00:14:41.760
As developers, we often craft functionalities across different contexts. In my work, I've found using modules to encompass functionality works wonders when interacting with external services. Each service can adjust its methods based on its needs, allowing flexibility without cluttering the overall code with excessive classes.
00:16:32.960
Expanding on object composition, proper implementation of validations and other meta-programming features enables you to achieve robust functionality. Good object composition practices will enhance your project while avoiding messy dependencies.
00:16:52.000
This strong emphasis on clarity and coherent object behavior management plays a significant role in Ruby’s effectiveness. Organizing highly functioning and readable code is incredibly valuable for any ongoing development project.
00:18:00.000
As we close, I'm eager to take any questions you might have. Many folks mentioned feeling that parts of Chad's talk were over their heads, so I’d like to provide clarity or elaboration on anything that has been discussed.
00:18:40.000
If there aren’t any questions, I hope this session helped clarify Ruby's object model … Excuse me, you had a question?
00:19:00.920
Someone reminded me to mention that while I typically caution against using instance eval due to its slow execution, it can be appropriate in certain contexts, especially with dynamic method definitions in frameworks like Ruby on Rails.
00:20:38.840
The approach for Ruby's Singleton classes offers a flexible methodology. If you're always mindful of performance implications of your methodologies—such as potential behavior confusion from Singleton methods—keeping efficient code will be vital.
00:21:17.360
For example, utilizing method injections on an object basis rather than class-wide can ensure that each object retains clarity of purpose without unnecessary alterations at the class level. This flexibility invites creative solutions, empowering developers to experiment with Ruby's possibilities.