Ruby 2 Methodology
This is a modal window.
Das Video konnte nicht geladen werden, da entweder ein Server- oder Netzwerkfehler auftrat oder das Format nicht unterstützt wird.
Formale Metadaten
Titel |
| |
Serientitel | ||
Anzahl der Teile | 66 | |
Autor | ||
Mitwirkende | ||
Lizenz | CC-Namensnennung 3.0 Unported: Sie dürfen das Werk bzw. den Inhalt zu jedem legalen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen. | |
Identifikatoren | 10.5446/37550 (DOI) | |
Herausgeber | ||
Erscheinungsjahr | ||
Sprache | ||
Produzent | ||
Produktionsort | San Antonio |
Inhaltliche Metadaten
Fachgebiet | ||
Genre | ||
Abstract |
|
Ruby Conference 201525 / 66
8
10
11
14
18
19
21
22
23
25
26
27
30
31
35
36
38
40
41
42
46
47
48
51
52
54
55
57
60
61
62
64
66
00:00
Computeranimation
00:33
Domänenspezifische ProgrammierspracheCodeLaufwerk <Datentechnik>Weg <Topologie>CybersexSummengleichungVererbungshierarchieWeg <Topologie>Mailing-ListeDomänenspezifische ProgrammierspracheWellenpaketWeb SiteAggregatzustandComputeranimation
01:21
DreizehnLaufwerk <Datentechnik>Web SiteWeg <Topologie>Rechter WinkelComputeranimation
02:15
Selbst organisierendes SystemProzess <Informatik>Open SourceGrenzschichtablösungProdukt <Mathematik>ComputeranimationXML
02:57
RechenschieberCodeGüte der AnpassungCodeComputeranimation
03:29
ComputervirusComputervirusComputeranimation
04:04
TropfenMinimumSystem FValiditätCodeComputeranimation
04:39
ASCIIDivergente ReiheDemoszene <Programmierung>KollineationVorzeichen <Mathematik>FehlermeldungKartesische KoordinatenComputeranimation
05:16
ASCIIKartesische KoordinatenSystemaufrufVorzeichen <Mathematik>Reelle ZahlCodeResultanteCOMComputeranimation
05:51
Rechter WinkelQuadratzahlPoisson-KlammerKernel <Informatik>Vorzeichen <Mathematik>Computeranimation
06:24
GamecontrollerWort <Informatik>GruppenoperationKernel <Informatik>Computeranimation
06:55
AliasingDynamisches SystemSystemaufrufKlasse <Mathematik>TypentheorieTypentheorieAbstimmung <Frequenz>DefaultComputeranimation
07:28
Offene MengeDefaultComputervirusSystemaufrufDefaultNormalvektorVariableSystemaufrufComputeranimation
08:16
SystemaufrufSpannungsmessung <Mechanik>VariableWort <Informatik>Prozess <Informatik>FehlermeldungSkalarproduktQuantenzustandComputeranimation
09:01
GamecontrollerFehlermeldungPatch <Software>Wort <Informatik>ViewerFehlermeldungKartesische KoordinatenGamecontrollerReelle ZahlComputeranimation
09:34
Patch <Software>Konvexe HülleDualitätstheorieSkalarproduktComputeranimationProgramm/QuellcodeXML
10:07
Patch <Software>MathematikPunktPatch <Software>MultiplikationsoperatorAppletProzess <Informatik>Computeranimation
10:48
AppletKlasse <Mathematik>InstantiierungComputervirusInstantiierungKlasse <Mathematik>Computeranimation
11:23
CASE <Informatik>CASE <Informatik>Patch <Software>Computeranimation
12:02
Patch <Software>CodeNotepad-ComputerParametersystemEigentliche AbbildungElektronisches ForumSoftwaretestMessage-PassingCodeCASE <Informatik>Rechter WinkelGamecontrollerGruppenoperationInstantiierungSystemaufrufParametersystemReelle ZahlComputeranimation
13:03
Patch <Software>ZahlenbereichPatch <Software>Arithmetisches MittelBestimmtheitsmaßBrennen <Datenverarbeitung>Rechter WinkelComputeranimationXML
13:42
InstantiierungCLIPatch <Software>DipolmomentGenerator <Informatik>Workstation <Musikinstrument>Patch <Software>XMLComputeranimation
14:16
Klasse <Mathematik>PunktDruckspannungComputeranimationXML
14:48
Patch <Software>Elektronische PublikationProzess <Informatik>Güte der AnpassungPatch <Software>Automatische IndexierungComputeranimation
15:22
Patch <Software>Objekt <Kategorie>SystemaufrufKlasse <Mathematik>Patch <Software>Objekt <Kategorie>SystemaufrufComputeranimationXML
15:55
Klasse <Mathematik>ComputervirusInstantiierungInstantiierungObjekt <Kategorie>MeterParametersystemKlasse <Mathematik>Computeranimation
16:39
ComputervirusInstantiierungMathematikSystemaufrufARM <Computerarchitektur>InstantiierungOrdnung <Mathematik>DifferenteKlasse <Mathematik>SchnelltasteComputerunterstützte ÜbersetzungTypprüfungFehlermeldungTypentheorieComputeranimation
17:28
ComputervirusBimodulInklusion <Mathematik>SystemaufrufKlasse <Mathematik>SchnelltasteBimodulDifferenteCASE <Informatik>MathematikHochdruckComputeranimation
18:05
ComputervirusBimodulInklusion <Mathematik>Klasse <Mathematik>Mobiles EndgerätKlasse <Mathematik>SchnelltasteBimodulPatch <Software>Computeranimation
19:02
ParametersystemVariableStellenringParametersystemSystemaufrufRechter WinkelVariableXMLComputeranimation
19:35
ParametersystemParametersystemAutorisierungQuantenzustandMultiplikationsoperatorQuick-SortComputeranimation
20:10
ParametersystemKonfiguration <Informatik>p-BlockParametersystemQuick-SortKonfiguration <Informatik>Umfangp-BlockWeb logOvalReelle ZahlComputeranimation
20:46
Selbst organisierendes SystemCodeGamecontrollerVersionsverwaltungParametersystemDigitalfilterReelle ZahlGamecontrollerGruppenoperationSpieltheorieWeb logFilter <Stochastik>ParametersystemRechter WinkelProgrammierungComputeranimationVorlesung/Konferenz
21:33
VolumenParametersystemsinc-FunktionAutorisierungFormale SpracheProgrammierspracheSchlüsselverwaltungParametersystemFunktion <Mathematik>XMLComputeranimation
22:17
DatenmodellGruppenoperationParametersystemComputeranimation
22:53
VariableBitStellenringVariableProgrammfehlerParametersystemUmwandlungsenthalpiePunktSchlüsselverwaltungWort <Informatik>MultiplikationsoperatorAggregatzustandComputeranimation
23:47
Wort <Informatik>ParametersystemSchlüsselverwaltungWort <Informatik>ParametersystemFehlermeldungRechter WinkelComputeranimation
24:29
VariableParametersystemTwitter <Softwareplattform>MaschinenschreibenVariablePunktMonster-GruppePatch <Software>Computeranimation
25:00
Patch <Software>Monster-GruppeSchnelltasteVariableWort <Informatik>Hash-AlgorithmusInstantiierungStellenringVariableSchnelltasteParametersystemRechter WinkelSystemaufrufInstantiierungHash-AlgorithmusComputeranimation
25:40
Hash-AlgorithmusVersionsverwaltungImplementierungPunktVersionsverwaltungPatch <Software>VariableMultiplikationsoperatorParametersystemWort <Informatik>Güte der AnpassungComputeranimation
26:31
Kette <Mathematik>sinc-FunktionPunktAliasingComputeranimation
27:16
ImplementierungDatensatzVersionsverwaltungAliasingImplementierungValiditätVersionsverwaltungPatch <Software>Zentrische StreckungFunktion <Mathematik>Computeranimation
27:55
DatensatzVersionsverwaltungPatch <Software>ValiditätRechter WinkelComputeranimation
28:34
Kette <Mathematik>DatensatzSystemaufrufKette <Mathematik>BitValiditätMaskierung <Informatik>ZeitzoneCASE <Informatik>Rechter WinkelComputeranimation
29:25
MAPFormale SpracheVererbungshierarchieDatensatzKette <Mathematik>PunktFormale SpracheMailing-ListeBimodulValiditätInklusion <Mathematik>FunktionalComputeranimation
30:01
VererbungshierarchieDatensatzCodeDickeVolumenvisualisierungAliasingReelle ZahlPersonal Area NetworkComputeranimation
30:43
VolumenvisualisierungDickeVererbungshierarchieDatensatzBimodulGruppenoperationWeb-SeiteVerschlingungComputeranimation
31:19
Patch <Software>BimodulPunktVersionsverwaltungComputeranimation
32:00
Gebäude <Mathematik>VererbungshierarchieZahlenbereichSystemaufrufKernel <Informatik>CodeBeschreibungskomplexitätVolumenvisualisierungVererbungshierarchieZahlenbereichDifferenteBridge <Kommunikationstechnik>VolumenvisualisierungXMLComputeranimation
32:47
Konfiguration <Informatik>VererbungshierarchieDatensatzDatensatzCodeVererbungshierarchieSystemaufrufPunktSpieltheorieObjekt <Kategorie>Computeranimation
33:23
SystemprogrammierungInformationSoftwareentwicklerAdditionVererbungshierarchieSystemaufrufDatensatzUniversal product codeValiditätMailing-ListeKette <Mathematik>AliasingBimodulObjekt <Kategorie>SchnelltasteVererbungshierarchieComputeranimation
34:14
SystemaufrufModul <Datentyp>VererbungshierarchieBimodulDatensatzTransaktionBimodulSystemaufrufVererbungshierarchieValiditätComputeranimationVorlesung/Konferenz
34:52
Modul <Datentyp>DatensatzTransaktionp-BlockObjekt <Kategorie>Rechter WinkelKlasse <Mathematik>VererbungshierarchieWeb-SeiteBitSystemaufrufValiditätPatch <Software>Mailing-ListeKette <Mathematik>Computeranimation
35:26
BimodulVererbungshierarchieProgrammfehlerZeiger <Informatik>BimodulProgrammschleifeKlasse <Mathematik>Message-PassingComputeranimation
36:12
CASE <Informatik>MomentenproblemBimodulKette <Mathematik>Klasse <Mathematik>CASE <Informatik>Message-PassingElement <Gruppentheorie>Patch <Software>ComputeranimationXML
36:47
CASE <Informatik>Framework <Informatik>Klasse <Mathematik>Patch <Software>Mathematische LogikFramework <Informatik>Computeranimation
37:24
ImplementierungElektronische PublikationPatch <Software>ZeichenketteBenutzerschnittstellenverwaltungssystemVersionsverwaltungElektronische PublikationBimodulComputeranimation
38:16
CASE <Informatik>SpeicherabzugKlasse <Mathematik>CASE <Informatik>Reelle ZahlSmith-DiagrammGruppenoperationSpieltheorieGamecontrollerPlug inComputeranimation
38:53
Elektronische PublikationProgrammbibliothekKlasse <Mathematik>SpeicherabzugElektronische PublikationRechter WinkelPatch <Software>Computeranimation
39:28
InstantiierungDynamisches SystemPatch <Software>Plug inWhiteboardSpieltheorieComputeranimation
40:17
PunktRechter WinkelComputeranimation
40:49
Computeranimation
41:24
SpeicherbereichsnetzwerkVideokonferenz
Transkript: Englisch(automatisch erzeugt)
00:15
So let me start my talk titled Ruby2 Methodology. This talk focuses on method in Ruby.
00:24
I'm sorry, but this talk is going to be a serious, pure Ruby talk. You know that RubyConf this year has these session tracks, like domain patterns, list code, with Ruby in depth, et cetera, et cetera.
00:42
And I think it's very well balanced, diverse topics. It's so, like, matured conference. But still, I'm so glad that RubyConf still has the Ruby in depth track. By the way, I guess you've heard of another conference
01:02
called Ruby Kaigi in Japan, which is happening next month. Yes? Thank you, thank you. Who's coming to Ruby Kaigi? Raise your hands. Mm, not so many, but yeah, thank you, thank you guys.
01:21
So the conference website looks like this. It has, the conference is a three days conference, and it has two tracks per each day, so it kinda has six tracks. And all of these six tracks will be just about Ruby.
01:42
Ruby, Ruby, Ruby, and Ruby. So, and Ruby Kaigi, everyone talks about Ruby because it's a Ruby conference, right? If you wanna see more Ruby talks, I'm sure Ruby Kaigi is the conference you should go.
02:04
Okay, and I guess you all wanna talk, I mean, hear about Ruby, so that's why you are here, right, so come to Ruby Kaigi, maybe next year. And the conference is organized by these people, and I'm at the very top as the chief organizer
02:27
of Ruby Kaigi this year, so that's me. My name is Akira Matsuda. I'm on GitHub as a Matsuda. I work for Ruby, Ruby on Rails,
02:42
and several other open source products, and consulting several companies. So, alright. I said this talk focuses on method in Ruby. I'm gonna talk about some modern usage of Ruby's method,
03:04
and I'm not just introducing these features like reference. I like to tell my own stories. I like to tell how I'm concerned, or what I did, or what I'm doing with these,
03:21
with lots of code examples. So, let's start with the method definition. To define a method, of course, we use def, for example, like this, def hello. But sometimes we want to define a method
03:41
with weird names, like, not like hello. For example, like emoji. Let's say, can a method name contain emojis? Who thinks that you can do this?
04:01
Okay, okay, thanks. The answer is yes. You can do, like, def beards. It's perfectly valid Ruby code, and you can see an actual code like this in an SfAerix active emoji gem, which looks like this.
04:22
At the bottom, it has def 10, def 100, et cetera, and it works. And you can just call these methods with your name, like, both beards, alright? Then, which character cannot be a method name?
04:45
You can imagine, like, some of these ASCII characters, like, one, two, three, or question mark, at sign, colon, semicolon. There might be invalid method name, for example,
05:02
defining an at sign method. This causes a syntax error. So, how can we define an at sign method without using C extensions? I actually do this a lot in my application.
05:24
This is a real code example for my application, defining at sign, exclamation, colon. So, to know which character is invalid method name,
05:43
just simply just try to define the method and catch the error, and the result becomes like this. Like, space, back quote, dollar, one, two, three, four,
06:03
at sign, square bracket. All of these can still be defined by a define method. So, how can we call this method? Of course, we have kernel send.
06:22
And that at sign method can be called via send, and kernel send is actually not only used for calling these abnormal name method,
06:41
but also for, like, determining the method name dynamically, or when calling a method from outside scope. This is an example of dynamically changing a method to call.
07:01
It's taken from Rails Action Controller. And another example of calling a method from outside scope is like this. It's defining a private method and calling it via send. So, what is method scopes?
07:23
There are three types of method scopes, public, protected, and private. Public is, of course, the default scope open to everyone. And private is like this. You cannot call private via normal method call.
07:43
It's very simple. Okay, and what's this? Ah, yeah, you can call the private method from the inside scope. Inside the class, right?
08:05
But if there is a local variable with the same name, the private method will never be called, but just the local variable will be called. So, how to call the method?
08:22
You can put parentheses to say that, like, to call the method, not the local variable. Well, sweet works, but it looks like a JavaScript.
08:42
So, no thank you. Another way to call the private method is to prepend self dot. Will this work? Unfortunately, no. It raises no method error.
09:06
We hit this situation on a real application at UBRG. So, we changed a public method in Rails controller to be private, then we saw this no method error,
09:20
because we put self dot, and it worked, and it used to work, well, while the method was public. And I think this should not be an error. So, we wrote a patch, like this.
09:41
Allow private method with self dot. This was written by this guy, Sotero. And Matt accepted this. It's okay to merge, that's what he said.
10:02
But, I'm sorry, I forgot to merge this in, and he said, please do the documentation, but I had no time to do this. So, the patch is done, but it's still not included in 2.3, maybe it's coming in 2.4.
10:24
Well, another way to call a method with self dot is change the scope to protected. Protected? So, what exactly is protected?
10:40
Who can explain, who here can explain? Protected is familiar in Java and C++, but Ruby's protected is different from these. And, well, it can't be called from outside,
11:03
like private, but it can call from other instance of the same class. That's the difference between private and protected.
11:24
So, who uses this feature? I searched for a real-world use case in Rails. As you know, Rails includes so many protected.
11:41
I saw more than 200 use cases, 200 occurrences protected in Rails, and I found almost all of these are wrongly used. I mean, actually means private, so I wrote a patch
12:07
to replace 150 protecteds to private, and it still works, but all the tests passes. So, that means more than 90% of protected
12:23
in Rails code actually means private, and the rest, like 20 or 30 use cases. This is the real use case of protected. Action controller parameters dupes,
12:45
and it calls the permitted method of another instance of duplicated parameters instance. This cannot be done if the method is private.
13:04
So, my advice is don't use protected unless you're sure you're calling protected method. However, again, this patch is still not merged because the patch has the problem, the rdoc problem.
13:25
Rdoc mutes private methods documentation. For example, if you have these methods and documentations, like public, protected, private,
13:41
the rdoc generates documentations for, sorry, public and protected, right, and does not generate documentation for private methods. So, my Rails patch, my Rails patch spoils so much documentations in Rails.
14:04
That's not good. So, this was the story of how we abuse protected, and why still we need protected in Rails. And, by the way, we found another rdoc problem in Rails.
14:24
Consider we have this class, like public def, protected def, private def. This is introduced from, I guess, 2.1. So, generating rdoc from this class
14:43
generates empty documentation. And even worse, if once private was defined, the rest of the methods become empty.
15:03
This was already filed on GitHub, rdoc rdoc, so we need a patch. Unless this issue is solved, we cannot use this syntax, private def.
15:24
So, we're looking for a patch. Please. And so, the next topic is method objects. Another way to invoke a method,
15:41
other than calling the method or sending to method, is method call. Call method of the method of objects.
16:02
You can inspect an object's method like this, and you can extract a method object from a class or instance, and you can call it this way. You can also pass in a parameter.
16:26
This is how you can get a method object from the class. Not from the instance. It's actually called unbound method.
16:40
It's kind of method, but you cannot call the unbound method. In order to call the unbound method, it's kind of different. So, in order to call the unbound method, you have to bind to an instance of that class.
17:01
For example, there's a person class and cat class, and each of these defines hello method, and are these hello method an instance of that? The answer is no. It causes type error.
17:21
So, you cannot unbind a method from person and bind it to a cat. It causes an error. But here's a new feature of Ruby 2 called method transplanting.
17:45
Well, you can unbind a method from the module, then bind it to a class. That's called method transplantation. It's nothing different from including the module
18:04
directly into the case, in this case, but it's kind of cherry-picking a method without including the whole module, right? And more new features in Ruby 2.2 is you can cherry-pick a method from a class
18:27
and bind to another class. Only if the method is from a module, originally comes from a module. This is so much useful for writing Rails monkey patches
18:47
because Rails has so many modules with so many methods, so you can just cherry-pick any method from modules and then put into your own class, right?
19:03
Another topic is parameter. As I said, you can pass in a parameter to method call. So, what's the parameter? It's like passing something from outside
19:23
and you can use the variable inside the method just like a local variable. So, how can we know what kind of method does a method do?
19:42
I'm sorry, parameters does a method take? Ruby 1.9 has method, parameters method, implemented by this guy, Koichi. He's of course, you know, you know who they are all.
20:00
He's the author of Ruby 1.9 VM called YARV, who's employed as full-time Ruby committer by Heroku. So, this is what's happening when you call methods parameter. It returns from sort of array over array,
20:24
something like this with an optional parameter and like this for args in block. So, how can we use this? I'm gonna show you a real-world example
20:42
of using this, method parameters. Let's say, rails-plugin, written by me, and it's kind of something to make your Rails controller act like Merb's controller, like this.
21:04
With this plugin, you can make your Rails controller to take a parameter. So, you don't have to access the params thing, right? And it supports filters before actions as well.
21:23
So, and, and it supports keyword arguments as well. So, what's keyword arguments? It's a new feature since Ruby 2.0, implemented by this guy, Mame.
21:43
He's, I guess, kind of known as the author of this thing Quine relaying for 100 programming languages.
22:01
So, this is the output of method parameter for keyword arguments. It's labeled like key and keyrest.
22:20
So, I recommend you all to use action args. It's very good. Actually, I cannot live without this when working on Rails implications. So, please try. And, by the way, I tried to bring keyword arguments
22:40
into Rails itself, like two, three years ago. I tried replacing some methods into keyword arguments like this.
23:01
Can you see this? I'm sorry, it's a little bit hard to see. Anyway, it's very much like clean, clear, API. However, I found a specification bug
23:21
on keyword parameters in Ruby 2.0. You know, Rails takes, sometimes takes, like if or unless keywords. And, it cannot be a keyword argument for Ruby 2.0.
23:41
Because, as I told you, an argument, a parameter is basically a local variable. And, local variable in Ruby cannot be accessed. It causes syntax error. So, you cannot define a argument, I mean, method parameter with a keyword.
24:03
Labeled lazy keyword, right? But, strangely, you can do this by a keyword argument. Yes, reserved word can be a keyword. And, you can actually call this.
24:21
You can pass in a if parameter into this method. But, I said, you cannot access to this variable. So, how can you use this? Which means you can create a local variable,
24:40
but you cannot touch it. It's totally useless, right? This is what happened in Ruby 2.0. So, I reported this problem. And, Koichi and Nobu fixed this. Nobu, of course, the patch monster.
25:04
So, how he fixed this? We introduced a new feature called binding.local variables and binding.local variables get. Now, you can access the if variable this way, right?
25:28
Another problem of keyword argument was performance. It used to be very slow because it internally creates hash instance per each method call.
25:40
But, Koichi fixed this in 2.2. He refactored the implementation and made it fast. So, I guess, in Rails 5, which probably will be coming soon, it supports Ruby only 2.2 and 2.3.
26:01
And, it's a major version bomb, so it's a good chance to break the API. And, now we have a way to access the if and unless variables. And, I think it's fast enough. So, I guess it's ready to introduce keyword arguments into Rails.
26:27
It's ready to, it's time to finish my patch. But, I'm sorry, I had no time to work on this, I guess, until Rails 5. So, someone please take this.
26:43
Now, the next topic is module prepends. Yes, Rails 5. Talking about Rails 5, alias method chain is deprecated. Yay! Yay!
27:03
For those of you who don't know what alias method chain is, it's a monkey patching idiom since Rails 1. Actually, Rails 0 point something. The very first implementation looks like this.
27:22
It's just a alias method and alias method. And, this is how you use this. So, you can override the safe method and add some new feature like validation.
27:42
This is the example. After calling the, after monkey patching, then calling the safe method, it outputs validated and saved. And, you still can call the unmonkey patch version,
28:01
the method that returns save only. It's named save without validation. It's very handy. All right? And, it's very intuitive method name. Save without validation, return saved. But, it actually has the dark side.
28:25
The dark side is save without validation doesn't always act as it really means. What if we have another chain like callback? Now, the save method does save,
28:43
sorry, callback and validation and save. And, let's call the save without validation method. It's not only skipping the validation, but it also skips the callback, right?
29:03
So, it's such harmful. Save without validation doesn't only skip validation in some cases and you cannot tell which case, right? So, the without method is just harmful.
29:20
Don't call this and don't even define this. So, we think we needed a nicer language support for monkey patching instead of a list method chain. And, that was introduced in Ruby 2.0 called module prepend.
29:43
How it works is basically like a reverse ordered module include. Module include is like this. You can include the validation functionality into the original save.
30:01
And, this is module prepend. You can do it the same way, but you can call, I mean, you need to call super from the included module, I mean, prepended module. This is how it makes it called code clean.
30:21
Like, this is the real world example of using module prepend. All of these used to be alias method chains, so there used to be tons of method definition, but using prepend,
30:49
it just looks like, you know, the only method defined here is render. And, yes, very simple, very clean, right?
31:02
So, module prepend always comes with send. I mean, like this, send prepend, because it was initially designed in the same way as module include, but I felt it's unnatural,
31:22
because it's for monkey patching, and monkey patching is always done from outside. So, why can't we call module prepend from outside? So, I proposed to make module prepend
31:42
and include public for Ruby 2.1, and it was accepted by Matz. So, you no more have to, like, send prepend or send include in Ruby, in recent version of Ruby. This is done by myself.
32:01
And, next topic is super, super method. This is numbers of super in Rails. It's so much heavily used, because how, because how it's Rails is designed.
32:26
But, since there are so many, like, def save super, def save super, def render super, def ren super, it's so hard to read through, and it's so hard to debug, like, you know, seeing the stack trace,
32:43
there are so many save, save, save, save, save, like this, active record has so many super. So, for example, when reading code and finding super,
33:03
how can I imagine, like, that super calls which super? So, there's a new feature in Ruby 2.2 called super method. It returns a method object, which will be actually called by super.
33:24
It's initially designed for debugging purpose, but I found that it's useful, not only for debugging, but for production code. So, let's get back to alias method chain.
33:42
I said there's no way calling save without validation, because there's no save without validation method defined in module prepends. But, actually, it's not true. You still can call the super method, because it's a method object, and you can bind, or you can call the method object.
34:04
So, this is how you can call the safe without validation. In module prepends. So, what if we have two modules prepended,
34:21
like this, validation callback? You can call super method super method, and still, you can call save only, right? So, what if you have N modules prepended? Now, method has owner,
34:41
which returns the owner called super method. So, it returns something like this, the module name, I mean module. So, you can iterate the super method until the owner returns the target class, right?
35:07
And, to generalize this a little bit, you can monkey patch method object like this, call without validation. You can call without validation using this method, okay?
35:21
So, you can still something like a list method chain, if you want to do it. But, sadly, I guess nobody uses super method, because it's still so buggy, and nobody reports the bug. For example,
35:41
unbod method has super method, but it returns nil for some reason. It's just a bug. And, by the way, I already reported this, and module prepend plus super method super method returns, it should return nil, but it like loops,
36:06
loops between the class and the module. So, it's another bug. This should be fixed, I guess, hopefully, before 2.3. I guess I have five more minutes
36:21
to talk about refinements. So, module prepend is a great tool to monkey patch. It's less polluting than element sub-chain, and it only, but it only can override
36:40
a existing method in the target module. But, that's not always the case. What if we want to monkey patch some class and include some module and define a method, some kind of private method there, and we don't want to expose
37:00
that internal method to the users? For example, like this. Extending some framework and doing some business logic, defining some logic inside, and we don't want to expose the bar method to users.
37:22
How can you do this? For this purpose, we can use refinements. Refinements is introduced from Ruby 2.0 by Shugo-san, who's Matt's boss, and it used to be something more powerful,
37:42
but the recent version of refinements is kind of file-scoped monkey patching tool. So, this is how you use refinements. For example, defining a monkey patching method
38:03
to a string, you can call this method only when using, I mean, declared to use that module. So, I'm gonna show you the real-world use case of refinements.
38:25
It extends Rails action controller base, and I define several methods there. But, you know, these are not needed
38:41
for Rails, essentially. So, I define these methods only for internal use inside this Rails plugin. So, I use these methods in another file in the Rails plugin using using,
39:05
and these are never exposed to the end users, right? So, this way you can create very, very clean monkey patch. I would call this super private method.
39:24
These methods will never expose to the end users, right? And exploring this feature, I found some pitfalls of refinements. Like, you cannot call the refine method
39:41
via send or public send. But it's, you know, we very often want to monkey patch some methods which is very often called by a send, particularly in Rails.
40:01
So, I really want to call the refine method in Rails, but I still want to use refinements for my Rails monkey patching plugins. I think this restriction is too hard
40:22
and should be loosened, in my opinion. So, I requested to please loosen this restriction. I actually asked Matt personally yesterday, and he said, maybe, maybe in 2.4, maybe?
40:45
So, I'm looking forward to this, to summarize. You know, this method is still getting better and better. So, I want you all to play with these new features
41:01
of method, and we can all make this better, make method better, to be more useful. So, let's hack on method and make Ruby more fun. Thank you very much.