RubyConf 2015

Novedades de los métodos de Ruby

Akira Matsuda  · 




Extracto de la transcripción automática del vídeo realizada por YouTube.

- Okay, let me start my talk titled "Ruby 2 Methodology. " This talk focuses on method in Ruby. 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, Less Code, Wetware, Ruby In Depth, et cetera, et cetera, and I think it's very well balanced, diverse topics.

It's so 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 called "RubyKaigi" in Japan, which is happening next month, yes? - [Man in Audience] Whoo! - Thank you, thank you.

Who's coming to RubyKaigi? - [Man in Audience] Woohoo! - [Akira] Raise your hands. Mmm, not so many, but yeah, thank you, thank you guys. So, the conference website looks like this. 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. Ruby, Ruby, Ruby, and Ruby. So at RubyKaigi, everyone talks about Ruby because it's a Ruby conference, right? If you wanna see more Ruby talks, I'm sure RubyKaigi is the conference you should go. (laughs) 'Kay, and I guess you all want to talk, I mean, hear about Ruby, so that's why you're all here, right? Yes, so come to RubyKaigi, maybe next year.

And the conference is organized by these people and I'm at the very top as the chief organizer of RubyKaigi this year. So, that's me, my name is Akira Matsuda. I'm on GitHub as "amatsuda. " I work for Ruby, Ruby on Rails, and several other open source products and consulting several companies.

So, all right. I said this talk focuses on method in Ruby. I'm gonna talk about some modern usage of Ruby's method, 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, 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 with weird names, like, not like hello, for example, like emoji. Can a method name contain emojis? Who thinks that you can do this? Oh, okay, okay, thanks.

The answer is "yes. " You can do, like, "def beers. " It's perfectly valid Ruby code, and you can see an actual code like this in sferik's active emoji gem, which looks like this. At the bottom it has, like, "def 10," "def 100," et cetera, and it works. And you can just call these methods with their name, like "puts beers," right? Then, which character cannot be a method name? You can imagine, like, some of these ASCII characters, like, one, two, three, or question mark, "at" sign, colon, semi-colon.

That might be invalid method name, for example, defining an "at" sign method. This causes a syntax error. So, how can we define an. . . "at" sign method without using C extensions? Actually do this a lot in my application. This is a real code example from my application: defining "at" sign, exclamation, colon.

So. . . to know which character is invalid method name, just. . . it's simply just try to define the method, and catch the error, and the result becames like this: like, space, backquote, dollar, one, two, three, four, "at" sign, square bracket. Of these can still be defined by a define method.

All right? So, how can we call this method? Of course, we have "kernel send. " And that "at" sign method can be called by "send. " And "kernel send" is actually not only used for calling these abnormal name method, but also for, like. . . determining the method name dynamically, or when calling a method from outside of scope.

This is example of dynamically changing a method to call. It's taken from Rails Action Controller. And another example of calling a method from outside of scope is like this. It's defining a private method and calling it via "send. " So, what is method scopes? 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. It's very simple, 'kay? And what's this? Uh, ah! Yeah, you can call the private method from inside the class, all right? 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.

[ ... ]

Nota: se han omitido las otras 2.086 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.