ActiveRecord

Summarized using AI

Insights from Metaprogramming Ruby

Mayra Lucia Navarro • October 25, 2022 • online

In this presentation from the WNB.rb Meetup, Mayra Lucia Navarro shares insights gained from the book 'Metaprogramming Ruby' by Paolo Perrotta. The session explores the concept of metaprogramming in Ruby, highlighting its capabilities and potential pitfalls for developers. Navarro defines metaprogramming as 'writing code that writes code,' emphasizing the flexibility that Ruby provides compared to more rigid programming languages like Java.

Key Points Discussed:
- Metaprogramming Definition: Metaprogramming allows developers to manipulate language constructs at runtime, enabling dynamic method generation and altering existing classes and methods.
- Active Record Example: Navarro illustrates metaprogramming through Ruby’s Active Record, showing how it reduces boilerplate code and allows seamless access to database fields without explicit definitions.
- Monkey Patching: An explanation of monkey patching demonstrates how Ruby lets developers reopen classes and modify methods dynamically, but highlights the risks of breaking existing code if not done carefully.
- Class and Object Hierarchy: Navarro discusses Ruby's object model, where every line of Ruby is executed within an object (self), and explores inheritance trees which show how methods are resolved.
- Dynamic Method Creation: The presentation details the creation of methods dynamically and using method_missing to handle calls for undefined methods gracefully, underscoring the power and flexibility of Ruby.
- Singleton Methods: The distinction between instance methods and singleton methods is clarified, with practical examples illustrating their usage within Ruby classes.
- Overall Philosophy: Navarro concludes with a recommendation to keep code simple and human-readable rather than overly complex, reinforcing the community-driven aspect of Ruby programming.

Takeaway: The discussion provides a deep dive into metaprogramming with practical examples, encouraging developers to leverage Ruby’s dynamic capabilities while also cautioning them about the responsibilities that come with such flexibility. Navarro’s enthusiasm for Ruby shines through, reflecting her commitment to fostering community knowledge and best practices in the Ruby ecosystem.

Insights from Metaprogramming Ruby
Mayra Lucia Navarro • October 25, 2022 • online

The book Metaprogramming Ruby by Paolo Perrotta changed my perspective on Ruby and led to the discovery of numerous better practices; I'd like to share some of what I've learned with the community.

Mayra Navarro is from Lima, Perú. Ruby and Rails developer for the last three years. Ruby Peru community's organizer. She enjoys Zumba classes when she is not coding with her cat. (Yes, her cat solves the bugs.)

https://www.wnb-rb.dev/meetups/2022/10/25

WNB.rb Meetup

00:00:01.079 okay so I've got to start this is me I
00:00:04.740 am from watcho City
00:00:06.980 of course I work in Target this
00:00:09.599 propagation and I also a reprove
00:00:12.240 organizer which is we with Katya that is
00:00:15.660 one number of woman or Winery also I
00:00:18.720 I've been working with Ruby and Ruby on
00:00:21.779 Rails since 2019 and I consider myself I
00:00:26.580 create c-cut lady
00:00:29.460 okay let's talk about what what is what
00:00:33.180 says Mars about meta programming it says
00:00:36.660 that
00:00:38.100 um
00:00:39.239 for developers we have a lot of tools
00:00:41.879 and one of them that we are provided is
00:00:44.520 the metac programming so every time that
00:00:48.239 we use it we have you we must be careful
00:00:51.300 about what we are doing with that
00:00:54.360 so let's start with the the m word
00:00:57.899 when we read the book we say the the
00:01:00.899 first
00:01:01.860 meaning about this is is writing quote
00:01:05.519 that writes code this is the the Isis
00:01:09.180 the simple definition for this
00:01:12.479 but for when we continue reading the
00:01:15.060 book we see something that how how our
00:01:18.780 construct the things based on languages
00:01:22.080 here we have for example how Java is the
00:01:24.900 when we create our code in Java we have
00:01:28.500 to use a compiler and that compiler
00:01:30.720 create a
00:01:32.220 another file that is going to be
00:01:34.380 released by our Builds on my team for
00:01:37.500 Java right so when we go to this point
00:01:41.520 there is anything that we can do with
00:01:43.439 our code is what is there is there
00:01:46.979 that's it you know we can't change
00:01:49.140 anything or even ask about what we have
00:01:51.899 there
00:01:53.220 but when we talk about Ruby that is
00:01:55.920 mentioning us a long time
00:01:58.079 language Constructor we can create a
00:02:01.680 class for example and then we can access
00:02:04.200 to that do this class just as giving the
00:02:07.979 asking asking about what it is what is
00:02:11.400 the object class what are their
00:02:13.500 variables for example
00:02:15.540 and that is what is called
00:02:18.120 are different
00:02:19.560 um for example if we have here
00:02:22.080 for example if we we have here the
00:02:24.300 active record how we mention it
00:02:27.120 and with this part we are just reducing
00:02:30.959 all the things that we have to do if we
00:02:33.180 see the
00:02:34.440 the great one
00:02:36.000 screen
00:02:40.319 so this is a active record approach that
00:02:43.260 was mentioned that only adding this that
00:02:47.340 is how we can create make a title and
00:02:50.580 this is something that is different
00:02:52.620 about the rest of languages because how
00:02:57.239 how the how Ruby knows in this case that
00:03:01.500 our table has a title column or it has
00:03:06.480 um director column or extra columns if
00:03:10.080 we I never told them about it but one of
00:03:13.980 the things that does acquired base
00:03:17.159 behind is just created that is what we
00:03:20.040 use called
00:03:21.659 um meta program uh that this is is made
00:03:25.680 in a long time
00:03:28.800 so
00:03:30.300 this is how this could be without the
00:03:33.659 active record approach a lot of codes
00:03:36.000 and we have to define the title for
00:03:38.340 example a director and then we have to
00:03:40.920 assign the values about it
00:03:43.980 and this is what I was mentioned it was
00:03:46.379 made on real Time That's where abuse
00:03:49.459 reduces a lot of time to recreate all
00:03:52.860 the things here
00:03:54.540 so just thinking about what we have seen
00:03:58.739 we can
00:04:03.319 say that manipulates language constructs
00:04:06.540 at runtime
00:04:10.319 so let's talk about the the object model
00:04:15.000 when we talk about Ruby we have always
00:04:17.820 have the the opportunity when a class is
00:04:20.820 created to open it again and add and
00:04:23.880 also edit the the methods that are
00:04:26.040 created inside
00:04:31.680 um for example here we have the class
00:04:34.080 integer and we have we opened that class
00:04:36.720 we include something extra kind of to
00:04:39.900 USD to to all the the dollar sign and
00:04:44.460 then we can call it in in some of our
00:04:47.580 new object classes to to add it
00:04:54.060 this is something that is called monkey
00:04:56.100 patch
00:04:59.100 but the problem is that sometimes if you
00:05:02.040 don't know what we we don't know what we
00:05:04.380 are doing we can
00:05:06.900 kind of break the the code that is
00:05:10.139 creating behind or we can change method
00:05:13.440 use in other pieces of coal for example
00:05:20.720 oh so let's talk about the objects here
00:05:24.960 those objects contains in some variables
00:05:29.340 um if we remember instance variables
00:05:31.500 exist only when we assigned in this day
00:05:34.320 object has made a is the instance for
00:05:37.440 them of the my class
00:05:41.400 and there
00:05:44.039 um if we can see the methods are located
00:05:46.680 in the class
00:05:53.639 but there is something that we need to
00:05:56.580 see we if we cause any
00:05:59.759 string here like ABC we see that the
00:06:02.699 class of that is a string but if we ask
00:06:06.240 about the class of the string it's
00:06:08.460 actually actually answered that method
00:06:11.759 class that what it shows us that it is
00:06:15.180 still a class is kind of the string is a
00:06:18.780 new Plus
00:06:24.000 um if we continue talking about what is
00:06:26.580 the superclutter of string it goes to
00:06:29.039 object and object if we ask it's upper
00:06:32.819 class it goes to basic object and then
00:06:35.880 if we go to separate class
00:06:38.160 to basic of a basic object it goes to
00:06:41.039 New because basic object is the root of
00:06:45.000 the rubber class hierarchy
00:06:51.600 so
00:06:55.319 if we talk about classes
00:06:57.720 we need to see that classes class the
00:07:01.740 class class is a module
00:07:04.020 and everything is inheritance from the
00:07:07.500 basic objects
00:07:13.139 so
00:07:16.319 there is here going we can't when I
00:07:19.319 found a question when do we use modules
00:07:22.440 and when do we use classes if both are
00:07:25.800 the same if a class is a module also
00:07:28.680 well the thing is in this book The the
00:07:32.160 there are also recommends to use
00:07:34.620 moduleplay we are going to include it in
00:07:36.840 some someone else for example a
00:07:38.940 calculator you can add it in the in a
00:07:42.539 class or when you have to manipulate it
00:07:45.620 and show it in a specific
00:07:48.960 format that is a way that we can use
00:07:53.360 modules but if we need to instant
00:07:56.639 something
00:07:57.780 sign up well in my mind I just think
00:08:00.780 about if you want to insert a cat for
00:08:03.240 example here you you need a class to to
00:08:06.360 to Define its name or the way or things
00:08:11.220 like that
00:08:13.319 so returning to classes are just objects
00:08:16.560 if we create to to
00:08:19.740 objects if we ask about his class it
00:08:23.580 goes to my class but if it goes to
00:08:27.060 his class is going to be class and if we
00:08:30.780 go
00:08:32.099 asking about their super classes and go
00:08:35.580 up up we can see what is we can see
00:08:38.880 these three that is called the inherited
00:08:42.500 inheritance tree
00:08:49.519 the ancestor changed
00:08:54.360 so for example if we have a place and we
00:08:57.779 have a child from that class
00:09:00.899 and we as about their sensors we have
00:09:03.720 the the class the initial class the
00:09:08.279 parent class the object class that we
00:09:10.740 showed before and also we it has the
00:09:13.860 kernel and the basic objects
00:09:19.200 and this is an example about if we have
00:09:21.959 a module where we Define a method and
00:09:25.380 then we included
00:09:27.000 and then we have a child that is
00:09:30.839 inheritance formed from C in this case
00:09:33.180 we have it in the in the
00:09:36.240 in the tree image of the right that the
00:09:39.779 module is the include makes it that it
00:09:43.019 is being called
00:09:45.120 before
00:09:47.339 the the object method are up
00:09:51.060 sorry after the
00:09:53.279 after the object method so every time
00:09:57.000 for example in this case when we call My
00:09:59.700 Method it first go to
00:10:02.459 to D and then it's see that it doesn't
00:10:06.360 exist by method then it goes to D to
00:10:09.320 first to D second to C and then it goes
00:10:12.959 to M until it finds it that and that is
00:10:16.860 how it shows there so next we talk about
00:10:22.140 Phil
00:10:25.080 um
00:10:26.220 and and here I it's just mentioned that
00:10:29.279 every line of Ruby is executed within an
00:10:32.459 object
00:10:33.360 so
00:10:35.040 the current object is called self
00:10:38.820 so it's only one object holds the cell
00:10:41.940 and anytime given an instance variables
00:10:45.420 and methods are called on self
00:10:51.180 and also in class or module definitions
00:10:54.240 the role of cell is taken by the class
00:10:56.700 or the module we are going to see
00:10:59.220 something like that later
00:11:01.740 so
00:11:07.620 this is something that is really cool in
00:11:09.720 Ruby it's when we can create or we can
00:11:13.680 call a method that if we didn't don't
00:11:16.200 even know the the its name
00:11:19.320 that is the that is one of the Magics
00:11:21.779 that would that comes with Robbie while
00:11:24.899 they call the code is running
00:11:29.459 but we need to be careful about the
00:11:31.560 method sent because it can call Private
00:11:33.779 methods too
00:11:37.500 so the other the other methods that we
00:11:41.220 can see here is when we can Define them
00:11:44.100 dynamically
00:11:46.440 for example in this case we have an
00:11:48.660 array of thoughts
00:11:50.519 um if we we can create an
00:11:52.800 a metal juice
00:11:54.899 defining them by their names
00:11:58.500 and here actually that is how we could
00:12:01.140 send for example the column names well I
00:12:06.240 was looking if that it was how active
00:12:09.420 record was but it doesn't but it's kind
00:12:12.120 of this that could be in an easy way
00:12:17.220 so another another thing that we can
00:12:19.860 find here is when we have the method
00:12:22.320 Missing Method meeting is it's a method
00:12:25.500 that is in the basic object in the model
00:12:27.660 basic object and here is when we can
00:12:31.800 make something special because if we
00:12:34.320 talk about for example in this example
00:12:37.019 if we call for a knock knock
00:12:41.060 methods that is the error that is going
00:12:43.800 to show us because it's on the final
00:12:45.660 method that is showing off that error
00:12:49.380 so what if we
00:12:52.100 open the method missing and we add our
00:12:57.120 extra lines of code to manage this kind
00:13:00.420 of error
00:13:02.160 and that is how we can
00:13:04.500 add an extra pieces of code and then
00:13:07.680 that is how can we the next time that we
00:13:11.399 talk sorry let's decide that we ask for
00:13:15.959 talk simple we are going to make our
00:13:18.899 modify
00:13:20.399 cold cold
00:13:22.620 so um this is the last part I want to
00:13:25.620 check that is I think this is the most
00:13:27.959 important that I could discover in this
00:13:30.600 book because it took me kind of one week
00:13:33.480 to read Page by page I think three times
00:13:37.740 because I I just was a little bit
00:13:40.560 curious to understand this concept
00:13:42.300 deeply deeply
00:13:46.200 for example when we
00:13:48.480 create two objects and then we have we
00:13:53.519 can't create uh methods a simple method
00:13:57.180 for one of the objects and then return
00:14:00.600 itself
00:14:02.639 that is what is the easiest way to
00:14:05.040 explain simple
00:14:07.980 single tone
00:14:10.920 methods
00:14:12.600 we add a method to a single instance of
00:14:15.420 an object that in this case is str1
00:14:18.720 string one
00:14:24.480 but it is something that we already have
00:14:28.079 seen in rails but we didn't know that we
00:14:30.899 were using Singleton methods for example
00:14:33.480 in this case we have a the class user we
00:14:37.260 have a method an instance method name
00:14:39.660 but then we have a Singleton method
00:14:43.680 called custom methods but there is
00:14:46.440 another
00:14:49.820 method and one of the things that I
00:14:52.740 discovered from this book is that this
00:14:55.440 is the most sophisticated thing to to
00:14:57.839 call a Singleton method
00:15:00.240 so the next time that we see something
00:15:02.639 like this
00:15:04.980 um we know that well I I finally nowhere
00:15:09.120 but it what it means
00:15:15.060 so but the thing is when we have a class
00:15:19.920 that this call is managed in this
00:15:23.420 ancestor chain
00:15:25.199 we have the object and each class is my
00:15:28.680 class and the suplica and the super
00:15:31.139 class of these
00:15:33.060 of this is object
00:15:35.040 but what happens well
00:15:37.680 um I forgot to mention that my methods
00:15:40.560 it's it's uh method allocated in the my
00:15:44.339 class
00:15:45.720 my class
00:15:47.639 so but
00:15:49.860 what happens when we have an inheritance
00:15:53.820 The Inheritance first goal to the to the
00:15:56.880 class to call the methods but it doesn't
00:15:59.880 find a method there so it goes up
00:16:02.880 following the ancestor change and then
00:16:05.579 it finds that it is in this the the C
00:16:08.820 class so there is where it is
00:16:12.240 that is what it is executed to show us
00:16:15.480 the result
00:16:18.060 so but what happens when we include a
00:16:21.120 Singleton class
00:16:22.740 for example in the here we have a single
00:16:25.800 two method
00:16:27.060 and we have added to the object
00:16:30.720 and if we ask about the singular class
00:16:33.360 it exists and if we see us about the
00:16:36.899 superclass of that single two class is d
00:16:40.860 so following is that the the ancestor
00:16:45.240 change has changed this time the object
00:16:48.360 first go to the single tone class that
00:16:52.139 is object and then it goes up to D and
00:16:55.800 then it goes up to C and that is where
00:16:59.100 it finds a method
00:17:04.140 so this is a piece of code that I could
00:17:06.839 find the book that I didn't want to to
00:17:09.540 make my own diagram so I made a copy
00:17:12.660 paste
00:17:13.740 so this is specifically how it is how
00:17:17.040 are the classes
00:17:19.980 really in Ruby every time that we create
00:17:23.939 a new object and remember the classes
00:17:26.640 are objects too uh I think the top class
00:17:30.059 is created it is hidden so it exists
00:17:34.860 for and that is here is when we have the
00:17:37.620 the change first object goes to the
00:17:40.740 class the Singleton class object and
00:17:44.340 then it goes to D to find a method and
00:17:47.220 then it goes to
00:17:49.380 it goes to the single Top Class to see
00:17:52.380 where it goes and try to find all the
00:17:55.799 things that here
00:17:58.380 that's why it took me like a one week to
00:18:01.080 understand all this because I'm trying
00:18:03.240 to translate all this diagram
00:18:06.360 here but at the end I just realized that
00:18:11.580 oh oh well also the author mentioned
00:18:15.240 that meta program is just for um all the
00:18:18.600 things that we have been doing behind
00:18:20.960 Dynamic like or you know methods to
00:18:24.539 create it was just
00:18:27.720 um
00:18:28.440 it was used single programs code that we
00:18:32.580 haven't used in behind but we didn't
00:18:35.100 realize about it and at the end it's
00:18:38.940 just
00:18:40.140 oh well their recommendations give it by
00:18:42.660 him is just try to make your call so
00:18:45.360 simple no don't try to complicated
00:18:48.000 things because we have to remember that
00:18:51.480 we are work we are
00:18:53.940 according for humans we are not calling
00:18:56.400 for machines
00:18:57.960 um we also
00:18:59.820 try to make the things
00:19:02.400 happy that is why that's why we choose
00:19:06.059 or personally choose Ruby to pass my
00:19:09.360 name from language
00:19:11.520 uh that's it thank you
Explore all talks recorded at WNB.rb Meetup
+20