Summarized using AI

Meta-programming for Dummies

Weiqing Toh • June 22, 2017 • Singapore • Talk

In this talk titled "Meta-programming for Dummies" by Weiqing Toh, a Software Engineer from the Ministry of Education in Singapore, the speaker explores the concept of meta-programming, particularly within the context of Ruby on Rails. Meta-programming refers to code that modifies other code at runtime, a powerful facility provided by the Ruby language. This session aims to educate developers, both novice and experienced, on the benefits, pitfalls, and best practices surrounding meta-programming.

Key Points Discussed:

  • Introduction to Meta-Programming: Weiqing starts by sharing his personal journey into software engineering and how he became familiar with Ruby and meta-programming through resources like the book "Metaprogramming Ruby". He emphasizes the learning curve and the intuition gained over time.
  • Benefits of Meta-Programming:
    • Conciseness and Readability: Meta-programming can help create more concise and readable code. For example, using attr_accessor in Ruby to automatically define getter and setter methods significantly reduces the amount of boilerplate code.
    • Dynamic Behavior: Meta-programming allows the dynamic modification of applications, enabling behaviors to be defined at runtime based on current conditions.
    • Domain-Specific Languages (DSLs): By employing meta-programming techniques, developers can create DSLs that improve readability and reusability in code.
  • Examples: Weiqing presents practical use cases:
    • Attribute Accessors: Demonstrating how the attr_accessor simplifies property definitions for based models in Ruby on Rails.
    • State Management: Explaining how meta-programming can help manage state transitions in objects, using examples from his own work regarding report models.
  • Common Pitfalls:
    • Complexity in Code: Meta-programming can introduce complexity if not designed correctly. Weiqing discusses the importance of understanding the object model and maintaining code readability.
    • Unintentional Side Effects: The danger of modifying class behaviors unknowingly through meta-programming can lead to difficult debugging experiences.
  • Best Practices: The speaker encourages developers to document well, write tests, and understand the implications of their meta-programming practices. He stresses that not all situations warrant meta-programming, and teams should consider their specific contexts.

Key Takeaways:

  • Meta-programming in Ruby provides powerful tools for code reduction and behavior modification but needs to be used with caution.
  • Ensuring clear documentation, thorough testing, and intentional design choices is crucial to avoid complexities and maintain readability.
  • Teams must evaluate the benefits of meta-programming against its potential downsides based on their particular codebase and team dynamics.

Meta-programming for Dummies
Weiqing Toh • Singapore • Talk

Date: June 22, 2017
Published: unknown
Announced: unknown

Speaker: Weiqing Toh, Software Engineer, Ministry of Education

The construct of the Ruby language allows for meta-programming, or 'code which modifies code at runtime'. However, meta-programming is a double-edged sword; as much as it is useful, it could very easily be misused by teams as well. In this talk, I will cover the benefits of meta-programming and some (basic) fundamentals (in the context of Ruby on Rails) and discuss pitfalls, anti-patterns, and considerations by teams before adopting meta-programming. Don't worry, this is easily digestible for rubyists of all levels!

Speaker's Bio

Weiqing is a software engineer at Experimental Systems and Technology Lab, an engineering team from Singapore's Ministry of Education. He works on prototyping and building apps to help Singapore schools be better at what they do. In his spare time, he enjoys doing yoga and getting a good dose of overseas hikes.

Event Page: http://www.reddotrubyconf.com/

Produced by Engineers.SG

Red Dot Ruby Conference 2017

00:00:04.350 okay so everyone's warmed up okay so
00:00:12.389 after nary one so my name is waiting I'm
00:00:16.090 actually top engineer Ministry of
00:00:18.370 Education Singapore so we do that for
00:00:20.290 schools to help them teach better so
00:00:23.020 today I'm going to talk a bit about meta
00:00:24.430 programming so first I think I want to
00:00:27.190 talk a bit about why I want to talk
00:00:29.170 about this right so so I wasn't a
00:00:33.040 computer science student and years ago
00:00:35.020 actually I got a job in software
00:00:36.339 engineer so of course the first thing
00:00:38.199 you do is to write code and this was my
00:00:40.329 face when I was reading real bills Xcode
00:00:43.780 right so I don't I don't really get
00:00:45.460 what's going on the next best thing you
00:00:47.499 do is just copy what the existing
00:00:49.420 modifier bit and then submit a pull
00:00:51.760 request and three correct so over the
00:00:56.289 last three years there's been some very
00:00:58.809 nice things that they have gone my way
00:01:00.100 very helpful team one of the other
00:01:02.679 things that happen also so that the team
00:01:05.080 that worked on actually circulated a
00:01:06.370 book so metaprogramming Ruby right and
00:01:09.540 so at that part time it wasn't a
00:01:12.189 priority for me I took one year to
00:01:14.860 finish this book right so that's my
00:01:17.049 place where my parties are but in fact
00:01:20.110 over this one year I think after reading
00:01:21.729 through and rethinking about things I
00:01:23.469 learned a bit more about will be right
00:01:25.509 it's quite similar to how urgently be
00:01:27.850 true that would be sorta the C source
00:01:29.560 code path for me reading through the
00:01:31.360 concepts you know in a story like
00:01:32.799 manners more palatable right and six
00:01:36.189 months ago actually attended Ruby HG
00:01:39.369 Mesa and dragos lost out there trying to
00:01:41.619 give a talk about submitting copper
00:01:44.140 proposed ourselves sitting out there
00:01:45.909 thinking about whether I should try for
00:01:47.740 something like that in six months
00:01:50.320 utopian right so this is my first top
00:01:52.689 four in a row because they're gonna
00:01:55.450 thank you for your assistance
00:01:57.130 yeah okay so I think the other thing
00:02:00.789 that I realized is that for meta
00:02:03.369 programming is for me if I don't do it
00:02:05.950 do it deliberately
00:02:06.969 it comes with bits and pieces as
00:02:08.380 intuition so what I want to do is to try
00:02:10.600 to actually identify certain key tips or
00:02:13.990 set and hope that both the junior
00:02:17.440 developers and a senior developers can
00:02:19.030 also pick up some of this and at least
00:02:21.300 benefit and keep the team
00:02:23.200 SM when you came so today I'm going to
00:02:25.810 talk about why we do meta programming
00:02:27.370 some kids and how not to get yourself
00:02:31.090 killed or do meta programming
00:02:33.480 okay so let's first start with the
00:02:36.640 definition of meta programming so Google
00:02:39.790 reviews many many kinds of definitions
00:02:42.780 the one I like I really like this code I
00:02:45.610 write small code but the more accurate
00:02:48.940 version of this is actually the writing
00:02:50.740 of programs there are multiplayer blah
00:02:53.380 blah so what it means is that if you su
00:02:56.550 as of now we write will be methods which
00:02:59.620 are set data and we give an output today
00:03:02.080 so meta programming means that you are
00:03:04.390 set the program that you have written as
00:03:06.430 a piece of data or an object and you
00:03:08.410 modify it so what you are doing is
00:03:10.510 you're inherently modifying the
00:03:11.860 application behavior at runtime everyone
00:03:16.240 got that ok so the next question said oh
00:03:20.140 this has a complex process so why do you
00:03:21.820 have a metaphor game in the first place
00:03:23.740 right so what I would try to do here is
00:03:26.860 to convince you that it is sometimes
00:03:29.680 better to make a program and I will try
00:03:32.350 to show some examples so we start with a
00:03:34.630 very easy one suppose I need I have an
00:03:37.510 object called a and I need to define the
00:03:41.230 standard getters and setters so the most
00:03:44.230 the easiest way to do this is just to
00:03:46.120 define a Ruby method as follows so just
00:03:47.890 as it goes get and set correct so this
00:03:52.090 is quite a few lines and actually what
00:03:55.090 happens is that you can actually rewrite
00:03:57.640 this in a you know nice length in only
00:04:00.850 by using attribute assessor right so I
00:04:04.390 think everyone's less familiar with
00:04:05.410 attribute Assessor
00:04:06.360 and if you want a very naive
00:04:09.760 implementation of this this is how it's
00:04:12.100 going to look like right but overall
00:04:14.020 what you have is that you have actually
00:04:15.970 extracted some of the logical ways right
00:04:18.220 so developed when developers will be
00:04:20.109 class ad instantly understand what foo
00:04:23.440 and bar actually means to the class
00:04:25.950 right so one of one of the benefits of
00:04:28.830 Mehta program is actually that you get
00:04:30.450 slightly more readable and concise code
00:04:32.160 in the class members in a class
00:04:35.220 definition okay so um another example
00:04:42.750 would be on my to Mehta program this
00:04:45.270 step in certain cases we have we might
00:04:48.720 want to extend this attribute access
00:04:50.910 level further so I guess how many of
00:04:53.340 y'all newsreels here many of you so if
00:04:56.760 you actually include active record base
00:04:59.160 in this manner you realize that rails
00:05:01.470 actually includes all the attributes for
00:05:03.480 you you do not even need to define them
00:05:05.820 and and as you edit the into the code
00:05:08.190 you realize that oh my reels does is
00:05:10.230 that it actually digging through this
00:05:11.610 the schema the database schema he
00:05:14.400 identifies this the set of attributes
00:05:17.780 that this model has and then it
00:05:20.760 populates that right and and so what
00:05:23.220 this does is that it gives you a very
00:05:25.890 awesome attic behavior and when you
00:05:27.750 define a typical classes these are
00:05:29.670 things that would help you along the way
00:05:31.380 so you don't have to spend a lot of time
00:05:32.880 writing it you don't really need to read
00:05:35.130 it because all these columns actually
00:05:38.310 listed in the schema schema and actually
00:05:40.830 all the developers are obey office okay
00:05:44.940 so another another value of
00:05:47.880 metaprogramming the following so suppose
00:05:49.560 we have a rapport model and and this
00:05:52.590 report requires three sticks so you have
00:05:56.880 a job are we doing in a published state
00:05:58.440 and then you want to transition between
00:05:59.850 the different states so what you have
00:06:02.820 this you want code that looks like that
00:06:04.770 on the left however I can see so you
00:06:09.720 have a state you create a new report it
00:06:12.270 is in draft scale what I can do is I can
00:06:14.670 try to submit it motion to release the
00:06:16.500 answer and sort of socialized a machine
00:06:17.940 alright so again a nice implementation
00:06:20.850 of this would be to just cut out the
00:06:23.850 methods as we can right but what happens
00:06:27.810 is that as business requirements change
00:06:29.940 you sub introduced a lot more things so
00:06:32.730 for example the boss comes student is no
00:06:34.620 I think three states is not enough we
00:06:37.170 might want to resize this
00:06:38.990 you know so what you can then do is to
00:06:41.880 go back to the class redefine right even
00:06:45.120 more methods but if you look at it
00:06:47.490 carefully
00:06:49.410 okay so again and so what can also
00:06:53.190 happen is that the boss could also ask
00:06:55.110 you for things like you know I want pre
00:06:57.210 and post transition hoops so for example
00:06:59.100 if I if I were to submit a report can I
00:07:02.130 actually send an email out to everyone
00:07:04.110 to just notify them of this and so and
00:07:06.750 so forth right you want to raise errors
00:07:09.060 so some of these are very common with
00:07:11.460 Pete about logic so what you can then do
00:07:14.310 especially meta program this right for
00:07:17.280 example if you can actually define the
00:07:19.340 state and the transition programming
00:07:22.680 program exactly right then what happens
00:07:26.280 is that you can actually write code to
00:07:28.170 actually generate these methods for you
00:07:29.790 automatically and the folks in there
00:07:31.980 will be a lot easier so what I'm going
00:07:34.290 to do here actually I've actually copied
00:07:36.200 what a gem called workflow has done so
00:07:39.750 what flow does is that it wraps up all
00:07:41.700 this logic into a very nice DSL as you
00:07:45.270 can see so what you have is that you
00:07:47.640 define the initial state then you try
00:07:50.400 the laser pointer money
00:07:52.380 okay memory so what happens that you
00:07:54.960 define state draft reviewing and publish
00:07:58.380 and then you define transitions between
00:08:00.780 them what you get here is that go a bit
00:08:05.040 that this code is more readable for
00:08:06.300 everyone so I think it's beautiful you
00:08:10.830 know because when currently we only
00:08:13.200 existed if you have ten states and penny
00:08:15.900 transitions between the ten states this
00:08:17.520 will look a lot more readable
00:08:19.110 then then having probably about 20 tons
00:08:22.950 about 100 200 methods in there right so
00:08:26.960 what you then do with metaprogramming is
00:08:29.520 then with this so what you do is that
00:08:32.370 you define domain domain-specific
00:08:34.260 languages and then you can hide the
00:08:36.060 logic elsewhere right so this is
00:08:38.070 definitely visible concise and of course
00:08:40.800 not reusable so now you can take this
00:08:42.719 workflow DSL and you can actually bring
00:08:45.450 it to other models and reuse it so just
00:08:49.170 to sum up I think these are just some of
00:08:51.360 the very
00:08:52.500 examples of why metaprogramming will
00:08:54.120 help you so there are more dynamic
00:08:57.210 they provide dynamic behaviors readable
00:08:59.790 concise you don't really repeat yourself
00:09:01.500 but I think all in all it provides code
00:09:06.570 that's a lot more readable I'm sure all
00:09:09.510 of you know that reading code that's not
00:09:11.550 done by you is actually quite painful
00:09:13.230 right so the more visible it is the less
00:09:17.040 pain you feel okay so I'm going to give
00:09:22.800 treated to start method opening so the
00:09:25.590 first is what GPS cover so when I saw
00:09:28.650 toggles correctly but it's actually what
00:09:31.050 you need to do is because for
00:09:33.660 metaprogramming what you are trying to
00:09:34.950 do is to modify the behavior in essence
00:09:38.100 you are trying to modify the behavior of
00:09:39.480 the object or the class so it is very
00:09:42.150 important for you to get your mental
00:09:43.560 model right so if you are to define
00:09:45.960 classes include classes subclass and and
00:09:48.180 so and so forth create objects right you
00:09:50.940 must be very clear on what you are
00:09:53.100 creating and what you're observing so
00:09:55.260 this is really ugly inside you get idea
00:09:59.330 so the first important thing is actually
00:10:02.040 for for people wanting more meta
00:10:03.750 programming to get this mental model
00:10:04.980 right if you don't get this right it's
00:10:06.630 very hard for you to implement proper
00:10:09.030 code in a meta programming sense okay
00:10:13.260 the second step to to actually drink
00:10:15.750 meta programming is to learn how to
00:10:17.220 modify the behavior a sub a sub point of
00:10:21.420 this is actually to understand how to be
00:10:22.980 evaluated so things like how are methods
00:10:26.310 being caught how do you how the messages
00:10:29.430 pass between the objects right what is
00:10:32.060 how do they catch missing message and
00:10:35.100 things like that
00:10:35.790 all right so a very good step for me I
00:10:38.430 assume is that you should read the code
00:10:41.580 will be dogs I would like to point to
00:10:45.180 two to set of functions that I think are
00:10:47.760 quite useful for everyone so if you do
00:10:50.880 need to understand a class or you need
00:10:53.640 to understand class you can use methods
00:10:56.250 like instance variable getter methods
00:10:57.990 one instance methods singleton methods
00:11:00.210 but a single class and if you need and
00:11:03.540 once you have if you need to understand
00:11:06.150 and you need to modify the behavior of
00:11:07.800 it then you can go on to the method on
00:11:10.650 the right so these are things like
00:11:12.000 defining method so you can program it
00:11:14.460 tickly define new methods you can add
00:11:18.510 methods or you can even and define
00:11:21.510 methods right so just a trivial for
00:11:24.330 those who are quite experienced these
00:11:27.450 methods are defined in very different
00:11:29.430 parts of movie so you need to do some
00:11:31.170 hunting if you want to find them okay
00:11:34.070 the last thing is is that it for you to
00:11:37.740 do whenever Graeme is to actually figure
00:11:39.900 out what's the DSL that you want so I
00:11:43.160 can't really give much advice that
00:11:45.840 because it's that context show depends
00:11:47.400 on the team but wise learn is that the
00:11:50.100 first copy is never the best you can
00:11:53.160 always integrate to get slightly better
00:11:54.750 DSL more readable code and what I would
00:12:00.930 suggest is actually that you copy for
00:12:03.360 mothers so I think wills has very good
00:12:06.060 implementations you can look through is
00:12:07.770 very very complex even for the attribute
00:12:10.950 accessor definitions but you can look
00:12:14.520 into some of these libraries or give you
00:12:15.990 an idea of how the dss are done and how
00:12:18.810 they are implemented okay so the problem
00:12:25.500 with meta programming is that if you can
00:12:27.300 modify behavior you can knowingly you
00:12:31.860 can also modify behavior unknowingly
00:12:33.630 right and that's where it gets very
00:12:35.550 dangerous right so simple things like
00:12:38.850 that can happen I have been caught many
00:12:40.800 times where my my teammates have with
00:12:44.580 vision code hidden in in a very small
00:12:47.250 folder somewhere and it which is often
00:12:50.940 very implicit behavior which I don't
00:12:52.290 recognize so it takes me a few hours to
00:12:54.450 debug which is kind of key right so I'm
00:12:58.740 gonna go through three big things so
00:13:00.630 some of the gotchas
00:13:01.350 so I think the first thing is su-metal
00:13:04.800 program try to keep the meta programming
00:13:07.890 code independent so if you do modify a
00:13:11.700 class or modify object please try to
00:13:13.830 keep it towards the object and not and
00:13:15.780 not introduce more dependencies
00:13:19.620 right the second thing is don't matter
00:13:22.380 program meta programming code so what
00:13:24.960 why can happen is that you can add a set
00:13:27.990 of defined methods they look very
00:13:29.190 similar I go oh why not let me just move
00:13:32.220 them together and I can do meta meta
00:13:34.050 programming right there if you I have if
00:13:37.650 this is that if you remember reading the
00:13:40.050 meta programming code it is actually
00:13:42.600 very hard to be meta programming code in
00:13:44.130 itself that's actually an extra loop
00:13:45.900 when you're hit to try to figure out
00:13:47.820 what's going on what you are creating
00:13:49.410 while you're modifying so try not to
00:13:52.200 make it someone readable and that's
00:13:54.420 necessarily yeah the 13 out the slaves
00:13:57.540 exceed that you know more is not always
00:13:59.580 good after this talk I don't think it's
00:14:02.670 right to just go and say let's go make
00:14:04.380 meth above them every day right so
00:14:06.830 imagine the following case where you
00:14:09.000 have a class and you have 20 includes
00:14:12.589 under in front of after just after you
00:14:15.450 define a class so if you if you have an
00:14:18.480 out encounter that is very hard for you
00:14:20.760 to identify which piece of code actually
00:14:23.060 defines this behavior or modify his
00:14:25.440 behavior here the second thing on the
00:14:28.950 talk about is readability so I have kind
00:14:32.190 of shown you ways in which meta
00:14:34.650 programming can help in vwt but there
00:14:37.709 are also cases where it might not have
00:14:39.150 so much so let me give an example I
00:14:41.550 actually found this code online from
00:14:44.459 this gem called restclient so we have on
00:14:47.820 the left a method programming kind of
00:14:50.910 example and we have on the right a more
00:14:53.010 explicit example so who who prefers the
00:14:56.880 meta programming and who prefers the
00:15:01.170 right side the Mike's this example okay
00:15:03.810 so from those arrays that has actually
00:15:06.000 so this is the range testing question I
00:15:07.800 presented this to my team I also got
00:15:09.600 kind of a half ass at the end of the day
00:15:11.880 I think it boils down to the team's
00:15:14.880 composition and it is about what the
00:15:17.339 skin feels right certain certain people
00:15:19.860 if you have a team that has various
00:15:22.529 developers who are in this while working
00:15:24.180 on the Signet for a long time it is okay
00:15:26.550 to have more meta programming code
00:15:28.579 versus the team which has more turnover
00:15:30.810 or prototype that may be explicit is
00:15:32.730 much
00:15:33.240 it's a lot easier to get up to speed so
00:15:38.569 so yeah I want to say is that shot it's
00:15:41.699 not way to be double right and sometimes
00:15:45.540 at some point you have the choice
00:15:47.040 between whether you want more implicit
00:15:48.420 or my explicit behavior okay the last
00:15:51.029 thing to note is always remember to
00:15:53.730 document and test because
00:15:55.679 metaprogramming squad will be it's very
00:15:58.050 important to actually explain what you
00:16:00.029 are trying to do right so I yeah I think
00:16:05.069 we we understand this with you have done
00:16:07.619 a lot of bills you should know that you
00:16:09.480 are you are using a lot of meta
00:16:10.920 bergamini in there before I excellent
00:16:12.990 objections all the callbacks and things
00:16:14.759 like that so the documentation helps a
00:16:16.439 lot in identifying what you actually
00:16:17.939 want to do the other thing is also to
00:16:20.879 write tests so when you introduce some
00:16:24.059 meta programming code right it is when
00:16:27.540 when you read a test it also got you
00:16:28.980 know Monica to get your behavior so you
00:16:30.839 can community confidence it's also
00:16:32.970 important for you to write it so that
00:16:36.209 you kind of tell readers what you are
00:16:39.089 intending to do with this piece of metal
00:16:40.769 programming code so if you see the test
00:16:44.129 below here you you can actually tell if
00:16:47.629 you can see tell like the behavior they
00:16:52.170 are trying to to bring out in this
00:16:54.240 particular model right and so this is
00:16:57.360 for people who don't really like the
00:16:58.769 documentation and if they prefer to read
00:17:00.420 test this is a much better platform for
00:17:02.279 them okay so I think oh I know I just
00:17:11.159 wanna say that actually meta program is
00:17:12.510 your - so it's not really a silver
00:17:14.370 bullet for anybody but in certain cases
00:17:16.380 it does work wonders and is quite
00:17:18.809 magical and the code when the code you
00:17:21.209 have refactor this is a lot more
00:17:22.860 readable right so I just want to share
00:17:26.399 that also be aware of some of the risks
00:17:29.429 that this might have right so when you
00:17:32.039 have when you have a lot of meta
00:17:34.020 programming there's a lot of implicit
00:17:35.159 big it's hard to debug in fact it also
00:17:37.799 takes a long time to actually design it
00:17:39.600 and to do this okay so it's everyone has
00:17:43.740 enjoyed this
00:17:45.140 I hope you did but and if you want to
00:17:47.240 look more into it I can introduce you to
00:17:49.250 this book in the book they would they
00:17:51.860 will go more detail into some of the
00:17:53.480 possible spells or design patterns that
00:17:55.490 you can use and I hope out of this
00:17:59.090 answers since violence so so we get more
00:18:02.510 readable code okay so if that's all then
00:18:07.610 I think thank everybody for listening
00:18:09.980 and not falling asleep yeah and any
00:18:13.309 questions is it possible
00:18:14.840 oops so yes
00:18:23.450 so we take questions now anyone oh yeah
00:18:33.429 yeah thanks for a cameo um basically my
00:18:37.850 thermometer screaming is that you say
00:18:39.650 makes more readable code but anyway it
00:18:41.510 seems like you're just shifting the
00:18:42.650 readability to it makes it readable for
00:18:45.440 the use of the API but you are that you
00:18:47.809 have additional complexity and where the
00:18:49.910 course being written which is why Rios
00:18:51.230 is so easy for beginners to pick up but
00:18:53.480 is so magical so how do you actually
00:18:55.970 integrate the meta programming part of
00:18:57.799 the application into your project you
00:18:59.510 give managers is separate no librarian
00:19:05.230 okay so how do we do it okay so okay
00:19:13.040 actually I have two sliced in China
00:19:19.760 okay I think yeah I was actually working
00:19:24.540 on this example to just try to show some
00:19:26.070 examples of this so for example in you
00:19:28.890 know that actually we have many many
00:19:30.630 models and suppose in one of these
00:19:33.660 models you want to attach okay so the
00:19:35.700 models have South my report said baby up
00:19:38.610 there pictures right and you have
00:19:40.200 article solutions many pictures and
00:19:42.480 these things at attachment site so one
00:19:44.730 way of doing it is the following where
00:19:46.290 you can define it so it's still part of
00:19:49.740 the application code right by hidden off
00:19:52.860 in a text editor
00:19:55.230 I think we keep it we keep it separate
00:20:00.480 okay in the app but separate from the
00:20:02.760 rest of the model and then we just test
00:20:04.650 it yeah so I reviewed that yes it does
00:20:09.090 it does hide some of the complexity away
00:20:12.930 but so that's why I think if you have
00:20:15.900 tested that properly I think is fine so
00:20:18.900 yeah I think I did a video the easy way
00:20:22.590 to unfurl it here alright thanks
00:20:34.340 thank you so much for your talk
00:20:36.990 I was curious to know you mentioned that
00:20:39.620 depending on certain teams
00:20:41.309 metaprogramming might be a wise choice
00:20:43.080 and sometimes not so I was curious to
00:20:45.990 know in your own team how do you
00:20:48.990 determine whether something is a good
00:20:52.409 candidate to be met a programmed and
00:20:54.059 when it should not be okay
00:20:58.610 it's all for us what happens is that
00:21:04.399 when the best kind of candidates are
00:21:08.490 those where you find yourself repeating
00:21:09.690 the same the same things over and over
00:21:12.360 again so if I for this example of
00:21:17.159 attachments right we actually had this
00:21:18.750 we have to repeat this probably about 20
00:21:22.409 or 30 models and I think that was when
00:21:24.720 we made more sense to actually do meta
00:21:27.450 programming on this right for think they
00:21:30.840 are very well defined and the behavior
00:21:32.520 is understood really you should do this
00:21:34.760 for things that they aren't clear
00:21:36.630 because the design the design portion
00:21:39.120 takes quite a long time to do right to
00:21:41.190 come out the DSL and to implement it so
00:21:43.110 for unclear requirements it is better
00:21:45.120 not to involve the much of meta
00:21:46.830 programming right so yeah cool thank you
00:21:56.810 I was wondering what your approach to
00:22:02.150 method missing because all of these
00:22:03.710 examples that is shown here or like
00:22:05.240 defined methods so this is pretty safe
00:22:06.920 meta programming as you're you're
00:22:08.930 defining some sub method and you know
00:22:10.640 that this method will be there
00:22:11.960 method missing is like in all the double
00:22:14.180 explore that there is you you call it
00:22:16.700 whenever the method doesn't exist so I
00:22:18.650 was wondering if you think that it is it
00:22:21.290 is recommended by you to use this
00:22:22.580 approach or you should be extremely
00:22:23.810 careful and use it only when you have no
00:22:26.120 other choice or as the exam wearily yeah
00:22:28.670 okay so I I didn't want to talk about
00:22:30.890 meta meta missing there was anti-pattern
00:22:34.340 on it online but I think I think going
00:22:38.090 to bash in the details but for me if you
00:22:40.340 ask me if you can avoid meta missing you
00:22:43.580 should avoid method missing at all cost
00:22:45.580 right because method missing is if I
00:22:49.850 will see as a global catch-all for all
00:22:51.860 our missing methods but to implement it
00:22:55.040 correctly you need to also implement
00:22:57.700 respond to missing if I remember
00:22:59.960 correctly because that will identify
00:23:01.730 whether method is actually missing or
00:23:03.680 not so you can implement you can catch
00:23:06.410 some of these missing methods but Ruby
00:23:08.840 doesn't know that this method is missing
00:23:11.030 or not and so that's why I think you
00:23:13.670 have to understand object model little
00:23:15.170 bomber two and and see how the message
00:23:17.360 passes yeah so if you can you should
00:23:20.300 avoid it but always good to understand
00:23:23.180 and know how meta missing is implemented
00:23:24.920 one more question do you have any
00:23:26.720 favorite DSL out there in the open
00:23:28.430 source some that you particularly like
00:23:30.410 how readable it is so so I think I
00:23:35.110 really like workflow because I think we
00:23:37.520 have about we do is about four you know
00:23:43.190 okay so there but I what one is actually
00:23:45.530 a like a assessment net so we have
00:23:48.560 answers and wishes for four or five
00:23:50.540 sticks and actually they can rotate with
00:23:52.760 each other so I should've up flow DSL is
00:23:54.680 a very nice example there was one more
00:23:57.170 recently that I came in contact with
00:23:59.750 which I really liked
00:24:01.130 which was
00:24:05.630 it had to do with some passing I forget
00:24:08.820 I I can't like I I can't answer then how
00:24:12.390 much just do it later when you away okay
00:24:14.880 awesome thanks hi anymore okay
00:24:36.100 hi thanks for the talk for the example
00:24:40.010 of the HTTP actions and metaprogramming
00:24:45.440 yeah what would your approach to testing
00:24:48.950 the methods defined in the public API
00:24:52.460 would you just do separate tests for you
00:24:55.730 defining the explicit method like have a
00:24:58.580 test forget put post and greet or test
00:25:01.760 one test one of the possible verbs if
00:25:05.450 they exist what what do you do usually
00:25:07.910 when you met a program like public
00:25:10.700 methods in the objects okay so um okay
00:25:15.230 that different like examples um so okay
00:25:17.300 for this case what I might do is
00:25:20.960 actually to sort out the possible versus
00:25:24.350 just one but let the let the set of four
00:25:28.790 run and see are executed RBC's the
00:25:32.000 method call for something I did for the
00:25:35.300 workflow one wears a bit slightly
00:25:37.340 different now probably have a concrete
00:25:39.040 so I have a dummy class include some
00:25:41.600 workflows defined a methods defined
00:25:44.030 about fluid States and transitions there
00:25:45.560 now just has a test that thanks
00:25:49.250 yep Izumo can take one more question
00:25:55.070 before move on
00:26:01.550 okay thank you very much reaching let's
00:26:04.800 give them our laws
Explore all talks recorded at Red Dot Ruby Conference 2017
+12