Design Patterns
Delightfully Fashionable Lemurs in Decorating Ruby

Summarized using AI

Delightfully Fashionable Lemurs in Decorating Ruby

Brandon Weaver • November 08, 2022 • Denver, CO

In the video titled "Delightfully Fashionable Lemurs in Decorating Ruby," Brandon Weaver presents an engaging and whimsical exploration of Ruby's decoration patterns through a narrative involving magical lemurs. The talk, delivered at RubyConf 2021, integrates storytelling with programming concepts, making it accessible and entertaining for developers.

Key Points Covered:
- Introduction of Characters and Setting: The story begins with a young lemur named Red who needs new robes for his graduation. The narrative introduces magical characters like Professor Scarlet and the mysterious Indigo, who guide Red on a fashion adventure to Lavenville Row, a hub of style for lemurs.
- Adventurous Journey: The journey is filled with humor and magical mishaps, showcasing the whimsical nature of Ruby programming. Red and his companions experience twists and turns as they navigate the forest, reinforcing a sense of wonder in learning programming.
- Methods of Decoration in Ruby: As the tale unfolds, the focus shifts to the technical elements:
- Method Decoration: Discussion on using method_added, where methods can be appended with additional functionality, akin to adding magic to clothing. An example is given about how methods can be optimized by caching their results, significantly improving efficiency.
- Dynamic Module Creation and Prepending: The concept of modules in Ruby is explored, explaining how prepend can alter the order in which methods are called. This allows for alterations that can be beneficial, especially for debugging purposes.
- Technical Mechanics of Ruby: Weaver dives into the mechanics of Ruby concerning method definitions, instance methods, and how dynamic methods can enhance coding practices while avoiding common pitfalls.
- Conclusion and Community Message: The narrative emphasizes the importance of community and collaboration in programming. Weaver expresses gratitude towards the Ruby community, reminding viewers of the fragile nature of such relationships and the value of fostering friendships.

Takeaways:
- The talk successfully blends storytelling with technical programming concepts, making it easier to grasp potentially complex topics.
- The importance of optimization through decoration techniques in Ruby can lead to more efficient coding practices.
- The Ruby community is vital, and nurturing it can lead to personal and professional growth for developers.
This unique presentation showcases creativity in teaching programming and encourages developers to explore Ruby's layered functionalities while fostering community spirit.

Delightfully Fashionable Lemurs in Decorating Ruby
Brandon Weaver • November 08, 2022 • Denver, CO

Delightfully Fashionable Lemurs in Decorating Ruby by Brandon Weaver

What to wear what to wear! The lemurs have a love for new robes and clothes, but these clothes are magical, and do some very interesting things. As it turns out decorating in Ruby can do rather unusual things to methods, classes, and even the lemurs.

Join the lemurs on a fashion-filled journey through decoration patterns in Ruby including tricks with method, define_method, method_added, and Module#prepend.

RubyConf 2021

00:00:10.160 So glad you could make it! Take a seat and get comfy because we're about to get started.
00:00:15.519 I have a little fun trick to share. This is a magic book—it's also my page-turner, quite literally! In fact, it has some surprising effects for us.
00:00:22.240 Who exactly am I, you ask? Well, quite simply, my name is Brandon. I work at Square and do a lot with Ruby. However, that's not quite why we're here today. We're here to hear a story—specifically, a particularly magical story.
00:00:36.000 Shall we get started? It begins with Part Zero: A Very Well-Worn Robe.
00:00:40.960 Our journey begins with the young lemur named Red. Over the past four years of adventures, his robes have literally gone to tatters. And that, dear audience, is where our story unfolds.
00:00:54.800 You see, Red is visited by his teachers, Professor Scarlett and Crimson, who are preparing him for graduation to become a senior lemur.
00:01:02.320 "There you are, Red! I see you’ve finally taken after me. I'm so proud. But maybe we need to do something about that hat first. Maybe a wider brim?" "They’re not that bad, Professor. They’re in perfectly good shape," replied Red. Professor Scarlett contested, "I feel those robes have seen better days. We must find something more appropriate than your apprentice robes for your graduation."
00:01:15.119 "But they’re broken in! They’re awesome! Why would you change that?" As Scarlett began to critique Crimson, a mysterious figure—a cat wearing sunglasses—burst through the door.
00:01:29.920 "Oh no, no, no, no! How delightful! Indigo, is that your cat? Yes, yes, yes! Now, stop right there. This won't do! You're a senior lemur now, Red. We need to commemorate this momentous occasion!" With a twinkle in her eye, the mysterious character, an etcher cat, proposed a lovely adventure.
00:01:40.080 "What you need is a visit to Lavenville Row, where the most stylish wall leavers from around the world gather. Suits, scarves, boots, hats—they have it all and so much more than you could even begin to imagine. I know; I’ve tried it, and it gave me a headache!"
00:01:58.560 "Wait, wait, wait! What? What? It's my turn! We get to take you on an adventure this time! I paid the author good money to get written into the story, and I won't have you ruining it!" With that, Indigo and Escher took young Red on a magical adventure to Lavenville Row.
00:02:01.040 The group of adventurers found themselves deep in a forest, hoping to reach Lavenville. Red couldn't help but wonder if they even knew where they were going.
00:02:05.120 "Well, if you cross a T and swing past the half moon... the gumdrop? Oh yes, that's why I left it!" exclaimed Indigo, as Red desperately took the map. "Give me that map! I need to find out where we are! I think we're lost!" However, disappointment crept in as Red realized it was a chaotic mess, rather than a proper map.
00:02:26.239 "Isn't an adventure by the sounds of it? Just look at all the fun we’re having already! Oh, this was so worth it!" While Red and Indigo continued bickering about directions, the Escher cat grew increasingly vexed with her lemur friends.
00:02:50.240 Now, you see, the Escher cat, a magical but impatient feline, finally had enough. With a snap of her paw, reality unfurled around the group of merry lemurs, pulling them deeper into the void. "Oh, I love this ride! This is the best day ever!"
00:03:09.600 As described earlier, this impatient magical cat became quite the convenient plot device. The concern now was, where exactly would they end up?
00:03:12.960 We find ourselves overlooking the mountain town of Lavenville, where the sky above has begun to shimmer. Out of the void, Red conveniently reappeared on a path leading directly to the town.
00:03:36.160 However, one of the others fell right off the cliff! "Now, now! Did you have to put me on the side of the cliff? You could have put me next to Red! That was mighty unkind of you!" With that, the merry band of lemurs continued on their journey in Lavenville.
00:03:48.160 They entered Lavenville and found themselves in the main way of Lavenville Row, home to many a fashionable lemur sharing their craft. It would be no exaggeration to say that much of lemur fashion was born in these very streets.
00:04:06.720 Red wandered, awestruck, glancing at each store, taking in the wonders of this enchanting place. Quite frankly, it was overwhelming! Indigo waltzed on ahead, pointing out the different stores.
00:04:25.680 "Come, come! So much to see and so little time! We'll need a new scarf, some boots—maybe two pairs! A robe—definitely three robes! And hats! Hats! Hats!" Indigo exclaimed, clearly getting carried away.
00:04:46.240 Red hurried to keep up, but Indigo became lost in his own monologues, muttering to himself with the Escher cat in tow. Suddenly, Red noticed something quite unexpected: a scarf flew straight out of a doorway and into his hands.
00:04:57.200 When he grabbed it, he found himself yanked into a shop. Meanwhile, Indigo and Escher continued on their merry ways.
00:05:12.720 Red stood in front of a particular store, clutching the scarf, when a familiar lemur, Saffron, emerged from the doorway. "Come quickly! Come quickly! Before he notices! Or you’ll never find what you seek here!" Red, initially confused, felt compelled by Indigo's enthusiasm and entered swiftly.
00:05:40.480 Just as he stepped inside, Saffron quietly shut the door behind them, ensuring not to attract any attention. A sign on the door banned Indigo and a certain quantumly entangled feline from entry, but that's a story for another day.
00:06:06.280 Meanwhile, let’s see where Indigo and Escher wandered off to. "My, someone's singing my praises! Yes, yes, I can feel it. What do you say to that, Red?"
00:06:09.440 Red remained quiet, as Indigo glanced down at Escher cat. "Did you happen to see where he went?" "No, I thought not. Ah well, he’ll turn up sometime later. That’s the way these things work, right?"
00:06:38.240 Might I interest you in some tea?" Saffron asked. "No, I feel like you're just distracting me. What—" Red interrupted, suddenly noticing a pearl oolong tea. "Oh yes, indeed! Only the best! Would you care to join me for a cup?"
00:06:50.320 Indigo and Escher were graciously distracted for the remainder of the talk as they enjoyed their drinks.
00:07:04.560 Part Three: A Peculiar Set of Tailors.
00:07:05.760 Red found himself inside the Vermillion Tailors, where a familiar lemur, Saffron, greeted him. "Oh, it has been a while! Perhaps four years since we last saw each other! What a fortune to find you!" As Saffron presented herself with a flourish, she continued, "I do hope Cerulean is doing well—but that’s not why you're here today, is it? No, no, I don't think so."
00:07:32.720 Red chuckled, acknowledging the well-worn and loved robes he had been wearing all this time. A note of urgency crept into Saffron’s voice: "You wouldn’t be here unless you intended to change that! Shall we take your measurements?" Saffron moved gracefully around the room as measuring tape floated around her like a dancer.
00:07:50.160 "Yes, yes, this will do nicely, I think. But alas, these robes bear no magic! Such a tragedy! Have they not been decorated?" Red was confused by this. "Magic? What do you mean? Are clothes not just clothes?"
00:08:10.320 At this question, Saffron came to a halt and looked directly at Red. "You see, clothing here can do far more than just dress. There are magics of all forms of decoration that can apply to your code and all manner of things!"
00:08:38.800 Red was fascinated and quickly asked, "Can one learn such magic?" Saffron smiled and replied, "I believe it’s no coincidence that you’ve come to our shop today! You can learn the arts of simple method decoration, method_added, and even those prepending methods that are the wonders of Ruby! You see, we spent many a year teaching these practices.
00:09:04.700 "Such exciting times. Curiosity must be rewarded!" With that, Red resolved to learn all he could from these mysterious tailors.
00:09:12.800 They began with the arts of simple methods and how they relate to methods. "Did you know that clothing speaks when it's created? And once it's spoken, we can add magics by listening to the true name of what we wear!"
00:09:16.800 Consider this boot; it’s been given magic to remember where it has traveled. It might also remember how long it has been used. There lies great power in names within Ruby, which allows us to bind magics to these powers.
00:09:37.760 For example, let's discuss a method that's very slow and expensive. If we're to call it more than once, it becomes rather inefficient! So how would we ensure it's not called more than once?"
00:09:53.840 There are a few ways, but let’s focus on using an instance variable to remember previous calls. If that instance variable is defined, we set a cache to remember it, and we return it later. If it’s not, we define that instance variable now.
00:10:18.000 "Now, what if I told you there are ways to use such magics without having to write a cache for every single method that requires one? And that, my dear audience, is decoration!" With that, they delved deeper into the concepts of decoration.
00:10:40.320 Three significant aspects that need to be understood were covered: what happens when a method is defined, the return of that method, and what happens thereafter. You see, when one defines a method—let’s say, "expensive_method"—did you know that the method's name is returned? It's actually returned as a symbol, which is important to note.
00:11:09.760 Now, where does decoration come in, you might wonder? Have you heard of the private keyword? All methods defined beyond the private keyword are declared private, regardless of how many methods it affects.
00:11:31.040 However, there’s another way to use it: we can prefix a single method we’d like to be private without having it apply to any other methods in the class. I personally prefer this approach for its explicit nature.
00:12:00.480 Now let’s consider our original "expensive_method" and what happens when we introduce another method before its definition—let's call this method "cache". By capturing the method name before its definition, we can do something with it just like private methods do.
00:12:40.640 To create a practical example, we want to use the instance method method, which interacts with instance methods of the class. For our lemur class, calling "expensive_method" through the instance method gives us back an unbound method. An unbound method must have an instance to operate upon, and since we’ve separated from the instance, we need to provide it to execute.
00:13:22.799 Thus, we need a way to bind this unbound method back to an instance, and that’s where rebind comes in. This gives us back a method object that behaves like a proc or function, allowing us to call it.
00:14:06.880 But why would we do this instead of directly calling the method? Bear with me! By storing a reference to the unbound method as the original method's name, we can define a new method or replace it later using define_method.
00:14:34.880 This way, we create a cache to store the result from the original method. Our approach gives us a simple and neat process to build our decorated methods.
00:15:06.240 In conclusion, the ability to decorate methods can save us from inefficiency whilst also giving us a sense of flexibility, especially when it comes to Ruby's unique magics.
00:15:25.919 One example surfaces: method_added, a hook called whenever a method is added to a class. This crucial hook captures the name of the method being added, but it's wise to control it using flags.
00:15:50.080 The greatest advantage lies in how we can leverage it to limit or expand modifications. Preventing potential disaster in using unnecessary methods leads to better efficiency within our code.
00:16:32.640 Our method decorations also extend to a great range of styles within Ruby, such as using private for narrow scopes or old school chaining with aliases. Such control and adaptability significantly impact ensuring clearer, stronger code.
00:16:59.840 Don't mind the fantastical story of Red and his adventures; it’s a representation of the beautiful world with unique frameworks guiding us. Yes! Such frameworks don't just apply to clothes. In the world of coding, the true understanding of one's work leads us to the core of its purpose and functionality.
00:17:36.679 With all that said, when Red tried on one of the robes and envisioned what magics might apply in the future, visions of enhancements and new styles filled his mind, bringing life to the previously static designs one might overlook.
00:17:55.520 As our story concludes, it is fitting to reflect on the commands that weave our thoughts and how magics best realized become enhancements, both on our journey and in the clicks of our keyboards.
00:18:30.000 It's time, unfortunately, to bid farewell as our journey must come to an end. "Thank you so much for your time," Red said, acknowledging the support he received from the community.
00:19:00.560 "I hope Indigo is enjoying himself!" someone asked. Red responded only by saying, "Oh, we're not entirely sure to be honest; this has never happened before!"
00:19:23.760 Before Red left, the instructor said, "Sharing tea with strangers is one of life's true delights! Take this hat as a parting gift; I think it will tie your outfit together nicely!"
00:19:56.160 And with that, Red began his journey home, thankful for the experience. As another tale was told, Red found himself reflecting on the many lessons learned throughout his adventures.
00:20:24.720 Days went by and still, Indigo and the Escher Cat did not return to the castle, creating a long silence. However, Red was unbothered, as he understood they were surely fine; that particular story would come later.
00:20:52.160 Red mused to himself, "Perhaps I should host a tea party to tie into this concept of monads everyone has been talking about."
00:21:22.160 Yet, he knew that in the world of lemurs, there were endless lessons to be embraced and friendships to be forged.
00:21:56.160 The conclusion, drawing connections towards community and its significance in our lives, was just one lesson that Red kept finding himself drawn back to throughout the years.
00:22:30.160 This talk today has been a labor of love, with completely original artwork and well over 140 hours of preparation dedicated to it, encompassing hundreds of slides along the way.
00:23:02.160 As always, if you'd like to know more or (contact me), feel free to reach out on social platforms and I’ll respond as soon as I can.
00:23:36.160 Lastly, I want to express profound gratitude towards the community for supporting me, collaborating, and always uplifting each other with their kind words. We must foster our communities carefully through love and kindness.
00:24:06.160 With that, Red concluded, "Let’s continue to make Ruby a beautiful thing together! Thank you so much!"
Explore all talks recorded at RubyConf 2021
+95