0:00:25.560,0:00:27.980 All right, good morning people. 0:00:27.980,0:00:32.520 After a bright talk this morning by Ajey,[br]it's 0:00:32.520,0:00:37.930 time to get dark and dirty, OK. So I'm 0:00:37.930,0:00:42.190 from Josh Software, and it started in 2007.[br]These 0:00:42.190,0:00:47.100 are my Twitter handles. And I'm also an author. 0:00:47.100,0:00:50.040 I have a couple of books which are out. 0:00:50.040,0:00:53.720 Well it doesn't look so dark anymore, does[br]it? 0:00:53.720,0:00:57.750 So what is my talk gonna be about? Don't 0:00:57.750,0:01:01.610 worry. It's not gonna be anything scary. I[br]love 0:01:01.610,0:01:06.570 Ruby. But as with every marriage, and I hope 0:01:06.570,0:01:09.150 there's no Ruby in the crowd - OK. As 0:01:09.150,0:01:11.170 with every marriage you also need to know[br]the 0:01:11.170,0:01:14.049 other side. You know the side you were - 0:01:14.049,0:01:16.840 you didn't know when you were just going around. 0:01:16.840,0:01:19.990 But it's the same thing with Ruby. You have 0:01:19.990,0:01:22.880 to know what you're looking at. And my talk 0:01:22.880,0:01:25.840 is going to be about the weirdness and the 0:01:25.840,0:01:29.549 gotcha moments. In short, in my talk, if you 0:01:29.549,0:01:35.689 find 'Ah-ha!' moments, it's working. Oh, we[br]have a 0:01:35.689,0:01:40.329 varied audience today. So to ensure sanity,[br]I have 0:01:40.329,0:01:44.119 tagged my slides. My slides are tagged with[br]my 0:01:44.119,0:01:48.920 good friend bumblebee to keep it neutral,[br]that these 0:01:48.920,0:01:52.149 are for beginners. So you must pay attention.[br]And 0:01:52.149,0:01:54.729 the experts, try to give the beginners a chance 0:01:54.729,0:01:58.990 to give the answer. And the experts, you know, 0:01:58.990,0:02:03.219 take your inspiration from Optimus Prime.[br]And the beginners 0:02:03.219,0:02:05.789 can choose to tune out, but whatever you learn, 0:02:05.789,0:02:09.970 the better. So let's get started with the[br]infamous 0:02:09.970,0:02:16.970 infinity. Now, we all know what infinity is,[br]right? 0:02:17.860,0:02:20.569 And since we are all programmers, obviously[br]we should 0:02:20.569,0:02:23.830 know the answer to this one. What's the answer? 0:02:23.830,0:02:29.090 There are no surprises here. No surprises[br]there. No 0:02:29.090,0:02:32.520 division by zero. We know this. What about,[br]what 0:02:32.520,0:02:39.520 about this? Oops. This actually works. So[br]we know 0:02:40.000,0:02:43.870 that everything in Ruby is an object. So that 0:02:43.870,0:02:45.540 looks like a, that looks like a class. So 0:02:45.540,0:02:47.760 let's see what the hell that is anyway. If 0:02:47.760,0:02:50.170 you type this out, what do you think I 0:02:50.170,0:02:54.659 get? So... what's going on here? So infinity[br]is 0:02:54.659,0:02:57.230 a constant define in the float class. Where[br]do 0:02:57.230,0:02:59.560 you see it, you don't see it there. I 0:02:59.560,0:03:01.989 can use this constant to form range comparisons.[br]I 0:03:01.989,0:03:04.629 can use it with equality operators and stuff[br]like 0:03:04.629,0:03:08.799 that. Imagine three equal to equal to infinity.[br]But 0:03:08.799,0:03:13.260 it works. Oh well. So. This is the way 0:03:13.260,0:03:15.829 I get people warmed up. Let's do something[br]with 0:03:15.829,0:03:19.450 more of an adrenaline rush. Base conversions.[br]It's a 0:03:19.450,0:03:22.930 lot of fun. So what do you think is 0:03:22.930,0:03:27.469 the output of this particular thing? Now,[br]I don't 0:03:27.469,0:03:29.640 want all the math geeks going to their calculators, 0:03:29.640,0:03:34.890 but essence is converting a number to a string 0:03:34.890,0:03:38.700 in the octal format. So again, no surprises.[br]Should 0:03:38.700,0:03:45.469 be fine. But let's push the tempo. Now what? 0:03:45.469,0:03:51.060 That, actually works. The next time you are[br]reading 0:03:51.060,0:03:54.439 about, and you hear a name like getafix, ?? 0:03:54.439,0:03:59.000 (00:03:57:07). You know oblix, asterix. It[br]need not be 0:03:59.000,0:04:04.620 a name, it could be a number. Well, now 0:04:04.620,0:04:11.620 what? Push the limits. Oops. So now if you 0:04:12.599,0:04:14.980 look at it a little logically, the radix as 0:04:14.980,0:04:17.720 a portion ?? (00:04:15:18) is supported only[br]in thirty-six, 0:04:17.720,0:04:20.858 because you have twenty-six alphabets and[br]ten digits. So 0:04:20.858,0:04:21.829 it can go only up to a radix of 0:04:21.829,0:04:26.240 thirty-six. So if there are some innovators[br]out there, 0:04:26.240,0:04:30.370 who want to actually get a new alphabet into 0:04:30.370,0:04:32.940 the English dictionary, we could probably[br]have a radix 0:04:32.940,0:04:39.940 of thirty-seven. OK. Let's move on to the[br]star. 0:04:41.180,0:04:43.870 Not the rock star, but the star operator.[br]And 0:04:43.870,0:04:46.180 to start with, let's see what it has to 0:04:46.180,0:04:50.150 do with Splat Expander. Oh boy, we see more 0:04:50.150,0:04:53.550 and more of Ruby code right now. So what 0:04:53.550,0:04:58.030 do you think is name and occupation here?[br]As 0:04:58.030,0:05:00.990 we can see we have a struct which takes, 0:05:00.990,0:05:05.120 which has two somethings, name and occupation,[br]and I 0:05:05.120,0:05:09.949 create an object off the struct. Now what[br]do 0:05:09.949,0:05:15.160 we see? Nothing fancy. We're still good. Right?[br]So 0:05:15.160,0:05:17.580 the Splat Expander is actually taking my arguments[br]and 0:05:17.580,0:05:20.600 given me the right name and the right occupation. 0:05:20.600,0:05:21.949 But we don't do things like this, do we? 0:05:21.949,0:05:25.930 We usually - we are Rails programmers. We[br]work 0:05:25.930,0:05:27.900 in Rails, so we usually have stuff like this. 0:05:27.900,0:05:31.639 We use keyword arguments. We all have keyword[br]arguments 0:05:31.639,0:05:34.520 to initiate a class like this and stuff. What 0:05:34.520,0:05:41.520 is the output now? It's Optimus Prime, so,[br]the 0:05:42.800,0:05:47.780 experts? What is name and occupation? Here[br]it's changed. 0:05:47.780,0:05:52.280 That's because the struct always has an array[br]of 0:05:52.280,0:05:54.800 arguments. It does not differentiate between[br]this. This will 0:05:54.800,0:05:59.009 not work on a class. Well, let's get to 0:05:59.009,0:06:04.490 something more funny. What about this? How[br]many here 0:06:04.490,0:06:07.889 have used this before? We know how to convert 0:06:07.889,0:06:09.729 an array into hash. How many of you have 0:06:09.729,0:06:13.590 used this notation before? All right-y, I[br]have two 0:06:13.590,0:06:17.780 people in the audience. Excellent. What happens[br]now? What 0:06:17.780,0:06:20.629 does this do? It actually converts to a hash. 0:06:20.629,0:06:23.729 And out of curiosity what if I add a 0:06:23.729,0:06:29.759 seventh element in the array? Error. No like,[br]dude, 0:06:29.759,0:06:31.069 I don't know what to do, what should I 0:06:31.069,0:06:34.330 do with the last one? Well, let's try some 0:06:34.330,0:06:37.800 more stars. Who all thinks that the output[br]of 0:06:37.800,0:06:43.460 this is gonna be three, six, and nine? Who 0:06:43.460,0:06:50.460 all think otherwise? What's the output? Awesome.[br]And now? 0:06:52.199,0:06:59.199 That was easy wasn't it? Now what? String[br]?? 0:07:03.370,0:07:08.310 (00:07:04:24). That's all actually works.[br]And let's take it 0:07:08.310,0:07:11.190 to the next level. How many here know about 0:07:11.190,0:07:14.830 Stabby, Stabby proc? Here is a sample. So[br]this 0:07:14.830,0:07:17.489 is a Stabby proc that we have which takes 0:07:17.489,0:07:21.750 - note we are still working with star. So 0:07:21.750,0:07:23.909 if I make an invocation ?? (00:07:23:07) to[br]the 0:07:23.909,0:07:30.909 block like this, what is the output? Mumbling,[br]mumbling, 0:07:32.550,0:07:39.550 mumbling. It's five. And now you say, I knew 0:07:40.259,0:07:42.379 that. Right. So basically the short form for[br]the 0:07:42.379,0:07:45.629 first, second last, last, and the rest of[br]the 0:07:45.629,0:07:49.289 stuff is in the middle. Pretty helpful thing[br]to 0:07:49.289,0:07:52.050 know. This works on all. It not, it's just 0:07:52.050,0:07:54.000 not worked with the Stabby proc, it'll work[br]with 0:07:54.000,0:07:58.180 Lambda, it'll work with a method, too. But,[br]as 0:07:58.180,0:08:00.930 with Ruby, and, if I'm supposed to be standing 0:08:00.930,0:08:03.870 here and I'm supposed to be lecturing you[br]on 0:08:03.870,0:08:06.919 all this, I'm supposed to know my stuff. Lo 0:08:06.919,0:08:11.090 and behold, when I was trying these experiments,[br]I 0:08:11.090,0:08:14.419 learned something new. What do you think is[br]the 0:08:14.419,0:08:18.870 output of that? That actually works, and I[br]have 0:08:18.870,0:08:22.289 freaking no idea how. So, well let's put in 0:08:22.289,0:08:24.770 the slide, and if I have somebody looking[br]explain 0:08:24.770,0:08:27.150 to me how does that dot notation just work? 0:08:27.150,0:08:29.490 And it works only on this. It will not 0:08:29.490,0:08:31.590 work on a method obviously, right. But it[br]works! 0:08:31.590,0:08:38.590 So, welcome to the weirdness of Ruby. Well.[br]Getting 0:08:38.620,0:08:44.240 onto really complex stuff. Case statements.[br]How many of 0:08:44.240,0:08:45.600 you have - you all have used cases, right? 0:08:45.600,0:08:47.240 Case when and stuff like that. Why do you 0:08:47.240,0:08:50.060 think this works? And I've tried to make the 0:08:50.060,0:08:54.580 example as complex as possible. All right.[br]If Optimus 0:08:54.580,0:08:56.680 Prime doesn't do this who the hell else would, 0:08:56.680,0:09:03.680 right? So what is the output? It's pretty[br]obvious 0:09:04.300,0:09:06.320 that we have a multiple of three, because[br]nine 0:09:06.320,0:09:08.329 is a multiple of three, and yeah, because[br]we 0:09:08.329,0:09:10.450 can all read in English and Ruby gives us 0:09:10.450,0:09:14.880 readable code. We know the output here, right?[br]Well 0:09:14.880,0:09:17.870 it's true. You're absolutely right. Ruby's[br]not that weird. 0:09:17.870,0:09:20.170 It'll give you the right answers. But my question 0:09:20.170,0:09:25.870 is: multiple_of is a method, right? It takes[br]one 0:09:25.870,0:09:30.430 parameter, which I have passed. How was it[br]compared 0:09:30.430,0:09:37.430 with nine? Where did nine come into the picture? 0:09:39.540,0:09:43.610 Behind every case, behind every successful[br]case, is a 0:09:43.610,0:09:46.790 case equality operator. So what actually happens[br]is that 0:09:46.790,0:09:50.480 that number nine is used with a case equality 0:09:50.480,0:09:54.839 operator. And that case equality operator[br]is an alias 0:09:54.839,0:09:58.380 to the proc call, which actually gets us our 0:09:58.380,0:10:02.970 output. Well. Though this seems pretty straightforward,[br]it has 0:10:02.970,0:10:08.550 just given me immense power. I can now manipulate 0:10:08.550,0:10:10.620 any way that I want a case equality operator 0:10:10.620,0:10:14.630 to work. Simply by over riding the equal to 0:10:14.630,0:10:16.240 equal to equal to method - the case equality 0:10:16.240,0:10:19.260 operator. And I am good to have any sort 0:10:19.260,0:10:21.610 of case comparisons that I want to make. I 0:10:21.610,0:10:24.329 don't care what happens, but it's my call[br]anymore, 0:10:24.329,0:10:28.079 right. Speaking of case equality operator,[br]let's go to 0:10:28.079,0:10:30.269 equality. How many of you all know these symbols? 0:10:30.269,0:10:34.040 There are operators, an eql question mark,[br]and equal 0:10:34.040,0:10:41.040 question mark. Let's have some fun. Any takers?[br]Who 0:10:43.430,0:10:46.459 all think the output is true? Who all think 0:10:46.459,0:10:50.170 the output is false? What happened to the[br]rest 0:10:50.170,0:10:55.360 of the people? Don't care. Don't care. That's[br]bad 0:10:55.360,0:10:56.950 for a conference. You know we just got a 0:10:56.950,0:10:58.230 huge lecture about participation! Again, who[br]all thinks it's 0:10:58.230,0:11:05.230 true? Who all think it's false? Why? Oh, come 0:11:05.480,0:11:09.380 on man, Ruby's gonna be really neat. Of course 0:11:09.380,0:11:14.519 it's true. Well, what do you think now? Anybody 0:11:14.519,0:11:18.940 think it's true? All right man. Stand up dude. 0:11:18.940,0:11:21.800 Please give him a welcome, because he's got[br]it 0:11:21.800,0:11:26.820 absolutely right. What the hell happened to[br]the rest? 0:11:26.820,0:11:29.399 Dude, we just saw the slide before about case 0:11:29.399,0:11:31.720 equality. Are you telling me if you use that 0:11:31.720,0:11:34.790 in a when statement it's not gonna work? Of 0:11:34.790,0:11:39.730 course it's gonna work. You're just comparing[br]to one. 0:11:39.730,0:11:46.060 And... now what? True? How many people think[br]it's 0:11:46.060,0:11:52.260 true? How many people think it's false? Why?[br]So 0:11:52.260,0:11:54.209 people called false but thought it was true[br]but 0:11:54.209,0:11:57.389 didn't want to answer. Well it's false, and[br]I've 0:11:57.389,0:11:59.110 got you guys so motivated you don't want to 0:11:59.110,0:12:02.959 answer, right. Damn it. But this is actually[br]equality 0:12:02.959,0:12:05.680 by value. So the value returned by the object 0:12:05.680,0:12:08.720 is one, the value returned by the float is 0:12:08.720,0:12:14.430 a floating point one. It's completely different.[br]Now what? 0:12:14.430,0:12:18.300 Now it's interesting. Why is it false? Somebody[br]said 0:12:18.300,0:12:25.300 very confidently, why is it false? Absolutely.[br]Now here's 0:12:25.430,0:12:29.100 the weirdness. This stuff actually compares[br]the object ID 0:12:29.100,0:12:30.829 of the two, and a integer and a float 0:12:30.829,0:12:34.670 would have two different object IDs. It's[br]obvious, right 0:12:34.670,0:12:40.040 fellas? So. Aiy aiy aiy, true false, what's[br]it 0:12:40.040,0:12:47.040 guys? There string 'a' and string 'a' are[br]two 0:12:47.240,0:12:52.329 different ruby objects. If I switch that two[br]a 0:12:52.329,0:12:54.600 symbol - thank you that's where you go. So 0:12:54.600,0:13:00.760 with that, let's hit the jackpot. How many[br]of 0:13:00.760,0:13:03.490 you all know what curry is? It's a lot 0:13:03.490,0:13:06.410 of fun, a lot of fun. But to make 0:13:06.410,0:13:10.389 it funnier, I actually decided let's have[br]the slot 0:13:10.389,0:13:13.940 machine get three pulls. And I wrote code[br]for 0:13:13.940,0:13:17.370 it on my flight here, so I have no 0:13:17.370,0:13:20.160 idea whether that's correct or wrong, but[br]like, we 0:13:20.160,0:13:23.579 know it seems a little weird though. I've[br]tried 0:13:23.579,0:13:26.579 to compare if all the three pulls are equal 0:13:26.579,0:13:31.730 and then I'm preparing some recipe with curry,[br]and 0:13:31.730,0:13:37.350 do you think it would work? It just looks 0:13:37.350,0:13:40.810 a little bit of weird code, but I shall 0:13:40.810,0:13:46.850 explain. So curry is a method in the proc 0:13:46.850,0:13:51.769 class which actually returns a lambda if all[br]the 0:13:51.769,0:13:55.010 parameters are not fulfilled. So in this particular[br]case, 0:13:55.010,0:13:57.790 I needed three parameters, X, Y, and Z, and 0:13:57.790,0:14:00.760 in the first statement, I passed only one.[br]Note, 0:14:00.760,0:14:04.040 the invocation is in square brackets. It's[br]different. And 0:14:04.040,0:14:06.720 it returns me a lambda, but other times when 0:14:06.720,0:14:09.420 all the parameters are actually fulfilled[br]in the second 0:14:09.420,0:14:13.420 statement, it actually evaluates the proc.[br]Now if you 0:14:13.420,0:14:15.610 had to write the same code you would have 0:14:15.610,0:14:18.459 taken different types of input, weighted for[br]the user 0:14:18.459,0:14:23.290 with (00:14:18:29) ...?? one, two, three,[br]and evaluated it. 0:14:23.290,0:14:24.920 You don't need to do that. And these things 0:14:24.920,0:14:31.920 can actually be very helpful. So curry away.[br]So! 0:14:34.300,0:14:41.300 So you think you can tell protected from private. 0:14:41.949,0:14:44.320 OK, I am not a - I'm a better 0:14:44.320,0:14:50.360 programmer than a singer. Right. Private methods.[br]All of 0:14:50.360,0:14:54.000 us have been taught since school, unfortunately,[br]that private 0:14:54.000,0:14:58.740 methods are not inherited. Are they inherited?[br]As you 0:14:58.740,0:15:01.259 can see, foo is a private method. And I 0:15:01.259,0:15:02.910 ran out of creativity, so I just went to 0:15:02.910,0:15:06.060 the standard base type foo, blah blah blah[br]here. 0:15:06.060,0:15:10.620 Is this going to work? All of us take 0:15:10.620,0:15:13.070 a lot of things for granted. How many of 0:15:13.070,0:15:14.420 you all knew this already, that private methods[br]are 0:15:14.420,0:15:18.500 inherited in Ruby? All right, OK, now I'm[br]scared. 0:15:18.500,0:15:21.160 How many of you all didn't know this? Don't 0:15:21.160,0:15:24.610 be shy, don't be shy. OK, so this is 0:15:24.610,0:15:28.079 actually one of the basic principles of where[br]Ruby 0:15:28.079,0:15:32.339 actually breaks traditional object oriented[br]concepts. All private methods 0:15:32.339,0:15:36.699 are inherited. What? How many of you all have 0:15:36.699,0:15:41.860 seen this code before? Forget the Mongoid::Document[br]but include? 0:15:41.860,0:15:46.029 Wait, come on. Everybody knows it! What is[br]include? 0:15:46.029,0:15:53.029 Is it a keyword? How many thought it was 0:15:53.139,0:15:55.120 a keyword? Oh it's like that, require or that 0:15:55.120,0:16:01.639 import in Java. Include is a private instance[br]method 0:16:01.639,0:16:07.500 defined in the class module. So while that[br]is 0:16:07.500,0:16:10.980 sinking in, it raises a basic question. All[br]private 0:16:10.980,0:16:17.980 methods are inherited. So what are protected[br]methods? So 0:16:18.269,0:16:19.889 what about protected methods? So what's the[br]difference between 0:16:19.889,0:16:23.089 private methods and protected methods now?[br]We were told 0:16:23.089,0:16:27.290 in our school that protected methods are inherited[br]but 0:16:27.290,0:16:32.110 not publicly accessible, right. Now private[br]methods are inherited 0:16:32.110,0:16:36.649 but not publicly accessible, right? So what[br]are private 0:16:36.649,0:16:41.130 metho- protected methods? Contrary to popular[br]belief, in Ruby 0:16:41.130,0:16:46.490 protected methods actually work with objects[br]and not classes. 0:16:46.490,0:16:49.610 And you can invoke a protected method on another 0:16:49.610,0:16:54.339 object within the same lineage. And I say,[br]what 0:16:54.339,0:17:01.339 the... what? Better explained with an example.[br]Simple example, 0:17:01.610,0:17:03.060 for the sake of brevity I put the initializer 0:17:03.060,0:17:09.230 in one line. What is the output, people? Don't 0:17:09.230,0:17:15.040 be shy. You have bumblebee to help you out. 0:17:15.040,0:17:19.990 No takers. Come on man, that's like pretty[br]obvious 0:17:19.990,0:17:23.050 right. You can't call a protected method on[br]an 0:17:23.050,0:17:30.050 object, right? Simple stuff. However. Now[br]beginners, so-called beginners 0:17:31.340,0:17:33.430 can tune out. This is fun. I added a 0:17:33.430,0:17:38.740 method called fights. Same piece of code.[br]What is 0:17:38.740,0:17:44.070 the output? Take a wild guess, man. Go for 0:17:44.070,0:17:51.070 it. So either Megatron or Decepticon. Any[br]other deceptive 0:17:52.350,0:17:59.350 ideas? Guys, very practically, a quick lesson[br]in human 0:17:59.880,0:18:02.710 psychology. If that didn't work, would I have[br]that 0:18:02.710,0:18:08.150 slide up there again? It works, man. It works. 0:18:08.150,0:18:09.940 But why did it work and how did it 0:18:09.940,0:18:13.430 work? The interesting part is that piece of[br]line, 0:18:13.430,0:18:15.660 earlier in the previous slide, prime dot nick[br]did 0:18:15.660,0:18:18.550 not work, but here target dot nick actually[br]works. 0:18:18.550,0:18:22.120 I have called a protected method on an object. 0:18:22.120,0:18:25.960 Now that same lineage means that because I'm[br]inside 0:18:25.960,0:18:28.950 the class Autobot, because I'm calling a method[br]on 0:18:28.950,0:18:32.320 the prime object, which is in, which is in 0:18:32.320,0:18:36.190 Autobot class, and since another object called[br]Megatron there 0:18:36.190,0:18:40.810 is of the same lineage, it's also an Autobot. 0:18:40.810,0:18:46.850 I can call the protected methods. So why didn't 0:18:46.850,0:18:49.900 it work earlier? Because everything in Ruby[br]is an 0:18:49.900,0:18:52.340 object. If you're trying to start prime dot[br]nick 0:18:52.340,0:18:55.140 and the previous case was outside the scope.[br]So 0:18:55.140,0:18:57.640 it was effectively in some main class, not[br]the 0:18:57.640,0:19:00.730 same lineage, so it doesn't work. So Ruby[br]actually 0:19:00.730,0:19:04.400 works in the traditional way, but has different[br]internal 0:19:04.400,0:19:07.380 meanings. And if now this was getting a little 0:19:07.380,0:19:10.430 complicated, how many of you all thought there[br]are 0:19:10.430,0:19:12.430 keywords in Ruby? Cause that man there is[br]gonna 0:19:12.430,0:19:14.260 come and shoot you if you think Ruby has 0:19:14.260,0:19:21.260 keywords. Does that code work? Is it even[br]valid? 0:19:23.700,0:19:29.640 Should it give me a syntax error? If, how 0:19:29.640,0:19:30.990 many of you think it will give me an 0:19:30.990,0:19:33.460 error? Syntax error too? What's wrong with[br]you? Have 0:19:33.460,0:19:37.070 you gone mad?-kind of errors. Any takers for[br]syntax 0:19:37.070,0:19:40.010 errors? Probably not. But if not, then I want 0:19:40.010,0:19:41.280 to ask you know what is the output of 0:19:41.280,0:19:48.280 this code? Seriously. There it is, seriously.[br]Question is, 0:19:50.440,0:19:51.710 how many of you all thought this is an 0:19:51.710,0:19:55.400 error called stack_too_deep. Cause when I[br]call false it 0:19:55.400,0:19:57.770 calls true, once you call true it will call 0:19:57.770,0:20:02.250 false. And it goes into a recursive loop and 0:20:02.250,0:20:08.330 case stack_too_deep. Any takers for stack_too_deep?[br]Higher, higher! And 0:20:08.330,0:20:12.240 how many think it will actually work? All[br]right. 0:20:12.240,0:20:15.510 The rest of the people, you all are beginners. 0:20:15.510,0:20:17.300 But yeah this works. So it does not stack_too_deep. 0:20:17.300,0:20:21.680 This actually works because true, true's resolution[br]is immediately 0:20:21.680,0:20:24.800 determined as the boolean value. However if[br]I had 0:20:24.800,0:20:29.650 changed these two particular statements, the[br]content in the 0:20:29.650,0:20:32.130 methods to self dot false and self dot true 0:20:32.130,0:20:39.130 I would probably get stack_too_deep. Not probably,[br]definitely get 0:20:39.190,0:20:42.270 stack_too_deep. OK. So. If this wasn't complex[br]enough for 0:20:42.270,0:20:49.200 you, let's go to modules, the mysterious modules.[br]You 0:20:49.200,0:20:51.030 know what the bull I have with my good 0:20:51.030,0:20:54.660 friend module, which has power, Megatron is[br]super powerful, 0:20:54.660,0:20:57.990 and he's a force, he's evil. And I want 0:20:57.990,0:21:00.910 to put that in my class Hanuman. note the 0:21:00.910,0:21:04.070 star, because that's true. I want the power[br]of 0:21:04.070,0:21:10.590 Megatron with Hanuman. Hanuman! Unfortunately[br]this will not go 0:21:10.590,0:21:13.460 down too well religiously for us, because[br]though it 0:21:13.460,0:21:15.890 will be Hanuman will be as powerful as Megatron, 0:21:15.890,0:21:19.960 Hanuman is now evil! So what do I do? 0:21:19.960,0:21:22.560 Is there a way I can cherry-pick from Megatron, 0:21:22.560,0:21:25.650 saying, I want the power of Megatron but I 0:21:25.650,0:21:32.650 don't want to be evil? What do I do? 0:21:33.060,0:21:35.700 I require. Note, I have not included Megatron,[br]I 0:21:35.700,0:21:39.980 require the Megatron method- Megatron file,[br]Megatron RB. I 0:21:39.980,0:21:43.270 define a method called power and I go to 0:21:43.270,0:21:46.190 the module, tell him to give me that instance 0:21:46.190,0:21:49.600 method called power, which gives me something[br]called an 0:21:49.600,0:21:54.030 unbounded method. And then I bind it to me, 0:21:54.030,0:21:59.730 that is self, and I call it. And that 0:21:59.730,0:22:06.730 makes my Hanuman religiously acceptable to[br]society. Question? No. 0:22:09.150,0:22:11.870 So note self, self is the object call. So 0:22:11.870,0:22:13.680 it's not working on the class call, it's actually 0:22:13.680,0:22:16.260 working on the instance. So every object,[br]every instance 0:22:16.260,0:22:21.660 of Hanuman, will actually get the power of[br]Megatron. 0:22:21.660,0:22:26.890 And with that I end my talk and thank 0:22:26.890,0:22:29.980 my two assistants here. I have, I'm open for 0:22:29.980,0:22:33.820 a few questions. Do I have the time, guys? 0:22:33.820,0:22:35.910 Satish, do I have some time? For questions?[br]Yay! 0:22:35.910,0:22:40.650 We have a lot of time for questions! All 0:22:40.650,0:22:47.650 right, you. Thank you. You have a question[br]there. 0:22:49.190,0:22:52.970 QUESTION: What exactly lead you to title this[br]talk 0:22:52.970,0:22:56.840 as the Dark Side of Ruby? G.R.: Oooh. Now 0:22:56.840,0:22:59.660 he killed me, didn't he? So that was a, 0:22:59.660,0:23:01.510 there's a two-sided answer to that. One was[br]to 0:23:01.510,0:23:06.020 fool the organizers, to get the CF?? (00:23:05:23)[br]accepted. 0:23:06.020,0:23:10.510 But more importantly, the dark side of Ruby[br]tries 0:23:10.510,0:23:12.130 to bring out the weirdness in Ruby, which[br]is 0:23:12.130,0:23:15.700 not the evil part, but the hidden part. Like 0:23:15.700,0:23:18.130 the dark side of the moon where we have 0:23:18.130,0:23:20.180 all these kind of stuff that breaks, the gotchas, 0:23:20.180,0:23:22.870 the moment that we feel that are going to 0:23:22.870,0:23:25.740 help but you don't really need to care about 0:23:25.740,0:23:28.160 it, but it's really, really important that[br]you know 0:23:28.160,0:23:32.840 these things exist. That's why Dark Side of[br]Ruby. 0:23:32.840,0:23:37.920 So I still love Ruby - no offense. Any 0:23:37.920,0:23:44.920 other questions? Yes sir. QUESTION: So have[br]you suffered 0:23:45.120,0:23:47.040 because of any of these gotchas in you work? 0:23:47.040,0:23:50.940 G.R.: Absolutely! This is all part of experience.[br]Part 0:23:50.940,0:23:55.860 (00:23:53:00 ??) is part of experience, part[br]is training, 0:23:55.860,0:23:59.310 and partly is preparation for my talk, trying[br]to 0:23:59.310,0:24:02.030 find out these terms, and figuring things[br]out. There's 0:24:02.030,0:24:04.170 a lot of stuff that I've actually kept out, 0:24:04.170,0:24:08.370 like you know ?? (00:24:07:03) games. Lots[br]of fun 0:24:08.370,0:24:10.850 with method missing. I haven't even touched[br]upon blocks 0:24:10.850,0:24:12.970 and ?? There's lots of funny stuff which happens 0:24:12.970,0:24:17.130 there. But I have only half an hour. So 0:24:17.130,0:24:18.450 the question to you guys also is that in 0:24:18.450,0:24:20.260 case you find some of these funny things which 0:24:20.260,0:24:23.040 happen in Ruby, and are able to explain it 0:24:23.040,0:24:24.770 or not able to explain it, send me a 0:24:24.770,0:24:27.580 Tweet, send me an email, and I will probably 0:24:27.580,0:24:30.600 investigate and improve my talk at your expense.[br]QUESTION: 0:24:30.600,0:24:35.380 Yeah. So the other question is, are there[br]some 0:24:35.380,0:24:39.850 practices, certain things that I follow, so[br]that I 0:24:39.850,0:24:42.730 don't get, I don't encounter these? G.R.:[br]Well, so 0:24:42.730,0:24:44.220 one thing, these aren't problems. What I wanted[br]to 0:24:44.220,0:24:45.900 point out here is that this is not a 0:24:45.900,0:24:47.390 bad part of Ruby, but these are the things 0:24:47.390,0:24:50.130 that we take for granted. So knowing about[br]protected 0:24:50.130,0:24:55.050 and private being different is important.[br]But not mandatory 0:24:55.050,0:24:57.910 for us to work as Rails programmers. But if 0:24:57.910,0:25:01.750 you know that, how protected and private actually[br]work, 0:25:01.750,0:25:05.050 it makes us better programmers. So best practices[br]of 0:25:05.050,0:25:07.450 Ruby are already there. You know there are[br]things 0:25:07.450,0:25:11.380 like flip-flops in Ruby which is for lack[br]of 0:25:11.380,0:25:15.220 a better term, a mind star star star star. 0:25:15.220,0:25:18.010 But, I do not recommend it. But it's there. 0:25:18.010,0:25:20.860 There are plenty of other things in Ruby,[br]too. 0:25:20.860,0:25:24.620 Which work, which should work, which you should[br]use. 0:25:24.620,0:25:26.970 The bottom line is that Ruby gives us the 0:25:26.970,0:25:29.530 maximum flexibility to build the way we want[br]to 0:25:29.530,0:25:33.050 build our code. So with great power comes[br]great 0:25:33.050,0:25:35.530 responsibility. And sometimes it's irresponsible.[br]So it worked out 0:25:35.530,0:25:39.040 great. Yeah. QUESTION: So is there a good[br]parts 0:25:39.040,0:25:41.460 of Ruby, like good parts of Java- G.R.: What? 0:25:41.460,0:25:43.890 QUESTION: Good parts of Ruby? G.R.: Oh, good[br]parts? 0:25:43.890,0:25:45.650 Everything! I've been working in Ruby for[br]six years 0:25:45.650,0:25:48.050 and I still love it. QUESTION: No, there is 0:25:48.050,0:25:49.320 a famous book called Good Parts of JavaScript,[br]a 0:25:49.320,0:25:51.600 very thin book that allows a subset of language 0:25:51.600,0:25:55.600 that is very safe to use, that you can 0:25:55.600,0:26:02.110 be very productive in. So do you, have you 0:26:02.110,0:26:02.200 come across something that says, like, there's[br]a subset 0:26:02.200,0:26:02.300 that you should stick to and use? G.R.: So 0:26:02.300,0:26:05.390 the best part about Ruby that I like is 0:26:05.390,0:26:09.070 the closures, which is completely misunderstood,[br]because every time 0:26:09.070,0:26:11.390 I talk to any person who's getting into Ruby 0:26:11.390,0:26:12.620 and I show them what a closure is they're 0:26:12.620,0:26:15.060 like yeah, that's a loop. But it's not. It's 0:26:15.060,0:26:17.670 like far more better than that. And it's far 0:26:17.670,0:26:20.660 more - it's more intuitive. It gets into the 0:26:20.660,0:26:23.410 details and gives us so much more incentive[br]about 0:26:23.410,0:26:26.160 learning how Ruby internals work, that we[br]tend to 0:26:26.160,0:26:29.100 actually ignore it in Rails. And I only (00:26:30:29) 0:26:29.100,0:26:31.530 have a lot of respect for people who have 0:26:31.530,0:26:33.010 learned Ruby and gone to Rails, because they[br]know 0:26:33.010,0:26:35.790 the right thing, other than most of us, including 0:26:35.790,0:26:38.340 me, who got into Rails and then fell in 0:26:38.340,0:26:41.770 love with Ruby. So it's different. QUESTION:[br]So is 0:26:41.770,0:26:46.310 Ruby the good parts the next book? G.R.: Could 0:26:46.310,0:26:48.090 be. But I'll need a lot of community support 0:26:48.090,0:26:54.900 for that. Yes? QUESTION: Yeah, hi, thanks[br]for the 0:26:54.900,0:26:57.390 talk. Just (00:26:55:09). I have been checking[br]out Exorcism 0:26:57.390,0:26:59.030 dot IO. I don't know if you guys have 0:26:59.030,0:27:01.750 heard of it. So it's actually started by Katrina, 0:27:01.750,0:27:04.000 who's part of the Ruby Rogues, and I have 0:27:04.000,0:27:06.530 had a wonderful time running stuff from Ruby,[br]like 0:27:06.530,0:27:08.940 I have been working Ruby for ?? (00:27:08:20)[br]for 0:27:08.940,0:27:11.650 years and also gotchas- G.R.: So, Exorcism[br]dot IO. 0:27:11.650,0:27:14.560 That's great. QUESTION: Like really good Ruby[br]idioms that 0:27:14.560,0:27:15.500 like so it's basically what happens is you,[br]there 0:27:15.500,0:27:17.210 are around ten languages if I'm not wrong.[br]You 0:27:17.210,0:27:21.160 can pick Ruby, you can sub in your solutions. 0:27:21.160,0:27:23.660 People can look at your code. They can give 0:27:23.660,0:27:26.810 good reviews. You can help out others with[br]your 0:27:26.810,0:27:30.190 suggestions. Yeah- G.R.: But to add to that,[br]there 0:27:30.190,0:27:32.370 is also some interesting - I'm not sure if 0:27:32.370,0:27:35.870 you all have heard of Winbgall (00:27:33:08).[br]Wingolf was 0:27:35.870,0:27:38.670 a gem and a simple tool where you can 0:27:38.670,0:27:42.150 report a problem statement, and using whim,[br]you know 0:27:42.150,0:27:46.150 execute that in the least number of key presses. 0:27:46.150,0:27:48.650 Similarly there are a few initiatives for[br]Ruby golf, 0:27:48.650,0:27:51.350 where you can actually try things like, the[br]beginner 0:27:51.350,0:27:54.210 ?? they actually said they have things with[br]?? 0:27:54.210,0:27:57.790 you know stuff like, write this piece of code 0:27:57.790,0:28:01.130 without using arrays, or without using the[br]equality operator. 0:28:01.130,0:28:03.500 And then you start, your mind starts churning,[br]and 0:28:03.500,0:28:05.090 then you dig deeper into Ruby and find other 0:28:05.090,0:28:07.140 things. So the best way to find these things 0:28:07.140,0:28:10.850 is to try Ruby ?? All right we had 0:28:10.850,0:28:13.380 a question there too. QUESTION: Not really[br]a question, 0:28:13.380,0:28:16.210 but just a suggestion, actually, to folks[br]who are 0:28:16.210,0:28:18.740 trying to learn Ruby. I like that he pointed 0:28:18.740,0:28:20.700 out that some of the things that we've learned 0:28:20.700,0:28:24.290 in college, classical logic and stuff doesn't[br]really apply. 0:28:24.290,0:28:27.750 What really helped me understand differences[br]was that treat 0:28:27.750,0:28:30.840 Ruby as a object-focused language and most[br]of the 0:28:30.840,0:28:33.120 languages that you probably end up working[br]with, Java, 0:28:33.120,0:28:36.280 C sharp, they'll be class focused. So concepts[br]like 0:28:36.280,0:28:39.340 private, protected, just like you mentioned,[br]are all object-level 0:28:39.340,0:28:41.580 concepts in Ruby. In fact if you wanted to 0:28:41.580,0:28:44.570 draw parallels again JavaScript is a good[br]parallel because 0:28:44.570,0:28:47.230 JavaScript is also an object-focused language.[br]So there are a 0:28:47.230,0:28:48.950 lot of parallels between JavaScript and Ruby, and 0:28:49.000,0:28:51.700 less between Java and Ruby. 0:28:51.700,0:28:52.700 QUERANT: Yeah, and- 0:28:52.740,0:28:55.700 It's like the dot, I'm sorry, that dot thing just amazed me. 0:28:55.700,0:28:58.780 ??, just having a dot. 0:28:58.900,0:29:01.460 and I was actually kind of disappointed when it 0:29:01.460,0:29:04.040 ended. I just wanted more. OK. 0:29:04.140,0:29:05.140 G.R.: Cool. Thank you. 0:29:08.260,0:29:09.260 Yes? 0:29:11.080,0:29:13.200 The Well-Grounded Rubyist. A must read. 0:29:14.260,0:29:15.680 V.O.: Somebody Tweet about it, please. 0:29:16.340,0:29:17.680 V.O.: We'll take one last question. 0:29:19.500,0:29:21.840 QUERANT: How does it help to have protected and private method 0:29:21.940,0:29:24.940 different from other languages? The way Ruby does it? 0:29:25.160,0:29:27.780 G.R.: I'm sorry. Did you ask what is different between protected and private- 0:29:27.780,0:29:30.140 QUERANT: No. How does it help to have a different design implementation, 0:29:30.140,0:29:31.140 to have it different. 0:29:31.140,0:29:33.720 G.R.: Oh. Excellent. And I love answering this question. 0:29:33.720,0:29:35.560 Though sometimes it can be a little long. 0:29:35.560,0:29:39.760 Why is private and protected different private and protected different in Ruby than 0:29:39.760,0:29:41.540 in other traditional standard languages? 0:29:41.540,0:29:43.160 Because Ruby did it right. 0:29:43.160,0:29:44.840 That's how it's supposed to be. 0:29:44.840,0:29:46.240 Example. 0:29:48.180,0:29:50.440 You know, when we talk about object-oriented programming, 0:29:50.440,0:29:52.680 it's supposed to be about objects. Things that you can see and touch. 0:29:52.680,0:29:55.060 For example: Inheritance. 0:29:55.060,0:29:57.020 Right. What is inheritance? 0:29:57.020,0:29:59.380 In, put it in, no uncertain term, though, it's my 0:29:59.380,0:30:01.080 ?? 0:30:01.820,0:30:04.900 Right. My inherited land my from my ancestors. 0:30:06.500,0:30:08.740 It's private. It's inherited. 0:30:08.740,0:30:11.620 You can. It, it comes within the family. 0:30:11.620,0:30:13.640 It can be protected. Can be private. 0:30:13.640,0:30:16.900 But, if you have your dad's wallet, do you inherit it? 0:30:18.280,0:30:20.440 You can choose to use it. You can choose not to use it. 0:30:20.500,0:30:21.620 It's just there. 0:30:21.620,0:30:24.980 The inheritance of land can be protected because it always stays in the family. 0:30:24.980,0:30:28.440 Like, for example, the money in the family is protected. 0:30:28.440,0:30:31.340 But that's always dealing with parents and children. 0:30:31.340,0:30:35.460 You can't go, your father. Just because you have a father and I have a father 0:30:35.460,0:30:37.000 I can't go asking your father for money. 0:30:37.000,0:30:40.700 Right. It's immense, how you determine. And of course public is the same everywhere. 0:30:41.260,0:30:43.960 So Ruby did it right, because it's, everything's object-centric. 0:30:44.680,0:30:47.940 Exactly what Aman said just now. Everything's object centric. 0:30:48.660,0:30:52.140 And, traditionally in object-oriented concepts, it started off right 0:30:52.140,0:30:57.020 and then it went all across the tangent, where you had this inheritance, and 0:30:57.020,0:30:59.120 classes and stuff like that. All the standard 0:31:00.060,0:31:06.480 things that I never talk about: incapsulation, abstraction, polymorphism 0:31:06.480,0:31:09.440 which seem to be the essence of object-oriented programming but is not. 0:31:10.240,0:31:11.840 V.O.: So, a follow-up question. 0:31:11.840,0:31:16.380 I have it. So is there a well-known pattern that makes use of, makes good of 0:31:16.380,0:31:18.200 private and protected? 0:31:18.980,0:31:19.980 We understand why- G.R.: Good qu- 0:31:19.980,0:31:20.980 QUERANT: Is it because it 0:31:20.980,0:31:24.400 G.R.: Include. Include. Include is an excellent example. 0:31:24.400,0:31:26.660 There are actually other patterns, too. 0:31:26.660,0:31:30.780 There are other patterns which allow for protected, protected is used very heavily for 0:31:30.780,0:31:34.080 all sorts of, the usually standard, why use protected, for example 0:31:34.080,0:31:37.180 protecting your password generation algorithm. 0:31:37.180,0:31:39.060 Something like that. But that's the deal with objects. 0:31:39.180,0:31:41.980 And since objects can be called only from their own lineage 0:31:41.980,0:31:43.940 you're always safe. Good question. 0:31:46.000,0:31:47.460 All right. V.O.: Anymore questions? 0:31:48.380,0:31:50.340 If there's anymore questions, we'll take it offline. 0:31:50.340,0:31:52.140 G.R.: Sure. Thank you so much 0:31:53.840,0:31:54.840 V.O.: Thanks Gautam.