1 00:00:25,560 --> 00:00:27,980 All right, good morning people. 2 00:00:27,980 --> 00:00:32,520 After a bright talk this morning by Ajey, it's 3 00:00:32,520 --> 00:00:37,930 time to get dark and dirty, OK. So I'm 4 00:00:37,930 --> 00:00:42,190 from Josh Software, and it started in 2007. These 5 00:00:42,190 --> 00:00:47,100 are my Twitter handles. And I'm also an author. 6 00:00:47,100 --> 00:00:50,040 I have a couple of books which are out. 7 00:00:50,040 --> 00:00:53,720 Well it doesn't look so dark anymore, does it? 8 00:00:53,720 --> 00:00:57,750 So what is my talk gonna be about? Don't 9 00:00:57,750 --> 00:01:01,610 worry. It's not gonna be anything scary. I love 10 00:01:01,610 --> 00:01:06,570 Ruby. But as with every marriage, and I hope 11 00:01:06,570 --> 00:01:09,150 there's no Ruby in the crowd - OK. As 12 00:01:09,150 --> 00:01:11,170 with every marriage you also need to know the 13 00:01:11,170 --> 00:01:14,049 other side. You know the side you were - 14 00:01:14,049 --> 00:01:16,840 you didn't know when you were just going around. 15 00:01:16,840 --> 00:01:19,990 But it's the same thing with Ruby. You have 16 00:01:19,990 --> 00:01:22,880 to know what you're looking at. And my talk 17 00:01:22,880 --> 00:01:25,840 is going to be about the weirdness and the 18 00:01:25,840 --> 00:01:29,549 gotcha moments. In short, in my talk, if you 19 00:01:29,549 --> 00:01:35,689 find 'Ah-ha!' moments, it's working. Oh, we have a 20 00:01:35,689 --> 00:01:40,329 varied audience today. So to ensure sanity, I have 21 00:01:40,329 --> 00:01:44,119 tagged my slides. My slides are tagged with my 22 00:01:44,119 --> 00:01:48,920 good friend bumblebee to keep it neutral, that these 23 00:01:48,920 --> 00:01:52,149 are for beginners. So you must pay attention. And 24 00:01:52,149 --> 00:01:54,729 the experts, try to give the beginners a chance 25 00:01:54,729 --> 00:01:58,990 to give the answer. And the experts, you know, 26 00:01:58,990 --> 00:02:03,219 take your inspiration from Optimus Prime. And the beginners 27 00:02:03,219 --> 00:02:05,789 can choose to tune out, but whatever you learn, 28 00:02:05,789 --> 00:02:09,970 the better. So let's get started with the infamous 29 00:02:09,970 --> 00:02:16,970 infinity. Now, we all know what infinity is, right? 30 00:02:17,860 --> 00:02:20,569 And since we are all programmers, obviously we should 31 00:02:20,569 --> 00:02:23,830 know the answer to this one. What's the answer? 32 00:02:23,830 --> 00:02:29,090 There are no surprises here. No surprises there. No 33 00:02:29,090 --> 00:02:32,520 division by zero. We know this. What about, what 34 00:02:32,520 --> 00:02:39,520 about this? Oops. This actually works. So we know 35 00:02:40,000 --> 00:02:43,870 that everything in Ruby is an object. So that 36 00:02:43,870 --> 00:02:45,540 looks like a, that looks like a class. So 37 00:02:45,540 --> 00:02:47,760 let's see what the hell that is anyway. If 38 00:02:47,760 --> 00:02:50,170 you type this out, what do you think I 39 00:02:50,170 --> 00:02:54,659 get? So... what's going on here? So infinity is 40 00:02:54,659 --> 00:02:57,230 a constant define in the float class. Where do 41 00:02:57,230 --> 00:02:59,560 you see it, you don't see it there. I 42 00:02:59,560 --> 00:03:01,989 can use this constant to form range comparisons. I 43 00:03:01,989 --> 00:03:04,629 can use it with equality operators and stuff like 44 00:03:04,629 --> 00:03:08,799 that. Imagine three equal to equal to infinity. But 45 00:03:08,799 --> 00:03:13,260 it works. Oh well. So. This is the way 46 00:03:13,260 --> 00:03:15,829 I get people warmed up. Let's do something with 47 00:03:15,829 --> 00:03:19,450 more of an adrenaline rush. Base conversions. It's a 48 00:03:19,450 --> 00:03:22,930 lot of fun. So what do you think is 49 00:03:22,930 --> 00:03:27,469 the output of this particular thing? Now, I don't 50 00:03:27,469 --> 00:03:29,640 want all the math geeks going to their calculators, 51 00:03:29,640 --> 00:03:34,890 but essence is converting a number to a string 52 00:03:34,890 --> 00:03:38,700 in the octal format. So again, no surprises. Should 53 00:03:38,700 --> 00:03:45,469 be fine. But let's push the tempo. Now what? 54 00:03:45,469 --> 00:03:51,060 That, actually works. The next time you are reading 55 00:03:51,060 --> 00:03:54,439 about, and you hear a name like getafix, ?? 56 00:03:54,439 --> 00:03:59,000 (00:03:57:07). You know oblix, asterix. It need not be 57 00:03:59,000 --> 00:04:04,620 a name, it could be a number. Well, now 58 00:04:04,620 --> 00:04:11,620 what? Push the limits. Oops. So now if you 59 00:04:12,599 --> 00:04:14,980 look at it a little logically, the radix as 60 00:04:14,980 --> 00:04:17,720 a portion ?? (00:04:15:18) is supported only in thirty-six, 61 00:04:17,720 --> 00:04:20,858 because you have twenty-six alphabets and ten digits. So 62 00:04:20,858 --> 00:04:21,829 it can go only up to a radix of 63 00:04:21,829 --> 00:04:26,240 thirty-six. So if there are some innovators out there, 64 00:04:26,240 --> 00:04:30,370 who want to actually get a new alphabet into 65 00:04:30,370 --> 00:04:32,940 the English dictionary, we could probably have a radix 66 00:04:32,940 --> 00:04:39,940 of thirty-seven. OK. Let's move on to the star. 67 00:04:41,180 --> 00:04:43,870 Not the rock star, but the star operator. And 68 00:04:43,870 --> 00:04:46,180 to start with, let's see what it has to 69 00:04:46,180 --> 00:04:50,150 do with Splat Expander. Oh boy, we see more 70 00:04:50,150 --> 00:04:53,550 and more of Ruby code right now. So what 71 00:04:53,550 --> 00:04:58,030 do you think is name and occupation here? As 72 00:04:58,030 --> 00:05:00,990 we can see we have a struct which takes, 73 00:05:00,990 --> 00:05:05,120 which has two somethings, name and occupation, and I 74 00:05:05,120 --> 00:05:09,949 create an object off the struct. Now what do 75 00:05:09,949 --> 00:05:15,160 we see? Nothing fancy. We're still good. Right? So 76 00:05:15,160 --> 00:05:17,580 the Splat Expander is actually taking my arguments and 77 00:05:17,580 --> 00:05:20,600 given me the right name and the right occupation. 78 00:05:20,600 --> 00:05:21,949 But we don't do things like this, do we? 79 00:05:21,949 --> 00:05:25,930 We usually - we are Rails programmers. We work 80 00:05:25,930 --> 00:05:27,900 in Rails, so we usually have stuff like this. 81 00:05:27,900 --> 00:05:31,639 We use keyword arguments. We all have keyword arguments 82 00:05:31,639 --> 00:05:34,520 to initiate a class like this and stuff. What 83 00:05:34,520 --> 00:05:41,520 is the output now? It's Optimus Prime, so, the 84 00:05:42,800 --> 00:05:47,780 experts? What is name and occupation? Here it's changed. 85 00:05:47,780 --> 00:05:52,280 That's because the struct always has an array of 86 00:05:52,280 --> 00:05:54,800 arguments. It does not differentiate between this. This will 87 00:05:54,800 --> 00:05:59,009 not work on a class. Well, let's get to 88 00:05:59,009 --> 00:06:04,490 something more funny. What about this? How many here 89 00:06:04,490 --> 00:06:07,889 have used this before? We know how to convert 90 00:06:07,889 --> 00:06:09,729 an array into hash. How many of you have 91 00:06:09,729 --> 00:06:13,590 used this notation before? All right-y, I have two 92 00:06:13,590 --> 00:06:17,780 people in the audience. Excellent. What happens now? What 93 00:06:17,780 --> 00:06:20,629 does this do? It actually converts to a hash. 94 00:06:20,629 --> 00:06:23,729 And out of curiosity what if I add a 95 00:06:23,729 --> 00:06:29,759 seventh element in the array? Error. No like, dude, 96 00:06:29,759 --> 00:06:31,069 I don't know what to do, what should I 97 00:06:31,069 --> 00:06:34,330 do with the last one? Well, let's try some 98 00:06:34,330 --> 00:06:37,800 more stars. Who all thinks that the output of 99 00:06:37,800 --> 00:06:43,460 this is gonna be three, six, and nine? Who 100 00:06:43,460 --> 00:06:50,460 all think otherwise? What's the output? Awesome. And now? 101 00:06:52,199 --> 00:06:59,199 That was easy wasn't it? Now what? String ?? 102 00:07:03,370 --> 00:07:08,310 (00:07:04:24). That's all actually works. And let's take it 103 00:07:08,310 --> 00:07:11,190 to the next level. How many here know about 104 00:07:11,190 --> 00:07:14,830 Stabby, Stabby proc? Here is a sample. So this 105 00:07:14,830 --> 00:07:17,489 is a Stabby proc that we have which takes 106 00:07:17,489 --> 00:07:21,750 - note we are still working with star. So 107 00:07:21,750 --> 00:07:23,909 if I make an invocation ?? (00:07:23:07) to the 108 00:07:23,909 --> 00:07:30,909 block like this, what is the output? Mumbling, mumbling, 109 00:07:32,550 --> 00:07:39,550 mumbling. It's five. And now you say, I knew 110 00:07:40,259 --> 00:07:42,379 that. Right. So basically the short form for the 111 00:07:42,379 --> 00:07:45,629 first, second last, last, and the rest of the 112 00:07:45,629 --> 00:07:49,289 stuff is in the middle. Pretty helpful thing to 113 00:07:49,289 --> 00:07:52,050 know. This works on all. It not, it's just 114 00:07:52,050 --> 00:07:54,000 not worked with the Stabby proc, it'll work with 115 00:07:54,000 --> 00:07:58,180 Lambda, it'll work with a method, too. But, as 116 00:07:58,180 --> 00:08:00,930 with Ruby, and, if I'm supposed to be standing 117 00:08:00,930 --> 00:08:03,870 here and I'm supposed to be lecturing you on 118 00:08:03,870 --> 00:08:06,919 all this, I'm supposed to know my stuff. Lo 119 00:08:06,919 --> 00:08:11,090 and behold, when I was trying these experiments, I 120 00:08:11,090 --> 00:08:14,419 learned something new. What do you think is the 121 00:08:14,419 --> 00:08:18,870 output of that? That actually works, and I have 122 00:08:18,870 --> 00:08:22,289 freaking no idea how. So, well let's put in 123 00:08:22,289 --> 00:08:24,770 the slide, and if I have somebody looking explain 124 00:08:24,770 --> 00:08:27,150 to me how does that dot notation just work? 125 00:08:27,150 --> 00:08:29,490 And it works only on this. It will not 126 00:08:29,490 --> 00:08:31,590 work on a method obviously, right. But it works! 127 00:08:31,590 --> 00:08:38,590 So, welcome to the weirdness of Ruby. Well. Getting 128 00:08:38,620 --> 00:08:44,240 onto really complex stuff. Case statements. How many of 129 00:08:44,240 --> 00:08:45,600 you have - you all have used cases, right? 130 00:08:45,600 --> 00:08:47,240 Case when and stuff like that. Why do you 131 00:08:47,240 --> 00:08:50,060 think this works? And I've tried to make the 132 00:08:50,060 --> 00:08:54,580 example as complex as possible. All right. If Optimus 133 00:08:54,580 --> 00:08:56,680 Prime doesn't do this who the hell else would, 134 00:08:56,680 --> 00:09:03,680 right? So what is the output? It's pretty obvious 135 00:09:04,300 --> 00:09:06,320 that we have a multiple of three, because nine 136 00:09:06,320 --> 00:09:08,329 is a multiple of three, and yeah, because we 137 00:09:08,329 --> 00:09:10,450 can all read in English and Ruby gives us 138 00:09:10,450 --> 00:09:14,880 readable code. We know the output here, right? Well 139 00:09:14,880 --> 00:09:17,870 it's true. You're absolutely right. Ruby's not that weird. 140 00:09:17,870 --> 00:09:20,170 It'll give you the right answers. But my question 141 00:09:20,170 --> 00:09:25,870 is: multiple_of is a method, right? It takes one 142 00:09:25,870 --> 00:09:30,430 parameter, which I have passed. How was it compared 143 00:09:30,430 --> 00:09:37,430 with nine? Where did nine come into the picture? 144 00:09:39,540 --> 00:09:43,610 Behind every case, behind every successful case, is a 145 00:09:43,610 --> 00:09:46,790 case equality operator. So what actually happens is that 146 00:09:46,790 --> 00:09:50,480 that number nine is used with a case equality 147 00:09:50,480 --> 00:09:54,839 operator. And that case equality operator is an alias 148 00:09:54,839 --> 00:09:58,380 to the proc call, which actually gets us our 149 00:09:58,380 --> 00:10:02,970 output. Well. Though this seems pretty straightforward, it has 150 00:10:02,970 --> 00:10:08,550 just given me immense power. I can now manipulate 151 00:10:08,550 --> 00:10:10,620 any way that I want a case equality operator 152 00:10:10,620 --> 00:10:14,630 to work. Simply by over riding the equal to 153 00:10:14,630 --> 00:10:16,240 equal to equal to method - the case equality 154 00:10:16,240 --> 00:10:19,260 operator. And I am good to have any sort 155 00:10:19,260 --> 00:10:21,610 of case comparisons that I want to make. I 156 00:10:21,610 --> 00:10:24,329 don't care what happens, but it's my call anymore, 157 00:10:24,329 --> 00:10:28,079 right. Speaking of case equality operator, let's go to 158 00:10:28,079 --> 00:10:30,269 equality. How many of you all know these symbols? 159 00:10:30,269 --> 00:10:34,040 There are operators, an eql question mark, and equal 160 00:10:34,040 --> 00:10:41,040 question mark. Let's have some fun. Any takers? Who 161 00:10:43,430 --> 00:10:46,459 all think the output is true? Who all think 162 00:10:46,459 --> 00:10:50,170 the output is false? What happened to the rest 163 00:10:50,170 --> 00:10:55,360 of the people? Don't care. Don't care. That's bad 164 00:10:55,360 --> 00:10:56,950 for a conference. You know we just got a 165 00:10:56,950 --> 00:10:58,230 huge lecture about participation! Again, who all thinks it's 166 00:10:58,230 --> 00:11:05,230 true? Who all think it's false? Why? Oh, come 167 00:11:05,480 --> 00:11:09,380 on man, Ruby's gonna be really neat. Of course 168 00:11:09,380 --> 00:11:14,519 it's true. Well, what do you think now? Anybody 169 00:11:14,519 --> 00:11:18,940 think it's true? All right man. Stand up dude. 170 00:11:18,940 --> 00:11:21,800 Please give him a welcome, because he's got it 171 00:11:21,800 --> 00:11:26,820 absolutely right. What the hell happened to the rest? 172 00:11:26,820 --> 00:11:29,399 Dude, we just saw the slide before about case 173 00:11:29,399 --> 00:11:31,720 equality. Are you telling me if you use that 174 00:11:31,720 --> 00:11:34,790 in a when statement it's not gonna work? Of 175 00:11:34,790 --> 00:11:39,730 course it's gonna work. You're just comparing to one. 176 00:11:39,730 --> 00:11:46,060 And... now what? True? How many people think it's 177 00:11:46,060 --> 00:11:52,260 true? How many people think it's false? Why? So 178 00:11:52,260 --> 00:11:54,209 people called false but thought it was true but 179 00:11:54,209 --> 00:11:57,389 didn't want to answer. Well it's false, and I've 180 00:11:57,389 --> 00:11:59,110 got you guys so motivated you don't want to 181 00:11:59,110 --> 00:12:02,959 answer, right. Damn it. But this is actually equality 182 00:12:02,959 --> 00:12:05,680 by value. So the value returned by the object 183 00:12:05,680 --> 00:12:08,720 is one, the value returned by the float is 184 00:12:08,720 --> 00:12:14,430 a floating point one. It's completely different. Now what? 185 00:12:14,430 --> 00:12:18,300 Now it's interesting. Why is it false? Somebody said 186 00:12:18,300 --> 00:12:25,300 very confidently, why is it false? Absolutely. Now here's 187 00:12:25,430 --> 00:12:29,100 the weirdness. This stuff actually compares the object ID 188 00:12:29,100 --> 00:12:30,829 of the two, and a integer and a float 189 00:12:30,829 --> 00:12:34,670 would have two different object IDs. It's obvious, right 190 00:12:34,670 --> 00:12:40,040 fellas? So. Aiy aiy aiy, true false, what's it 191 00:12:40,040 --> 00:12:47,040 guys? There string 'a' and string 'a' are two 192 00:12:47,240 --> 00:12:52,329 different ruby objects. If I switch that two a 193 00:12:52,329 --> 00:12:54,600 symbol - thank you that's where you go. So 194 00:12:54,600 --> 00:13:00,760 with that, let's hit the jackpot. How many of 195 00:13:00,760 --> 00:13:03,490 you all know what curry is? It's a lot 196 00:13:03,490 --> 00:13:06,410 of fun, a lot of fun. But to make 197 00:13:06,410 --> 00:13:10,389 it funnier, I actually decided let's have the slot 198 00:13:10,389 --> 00:13:13,940 machine get three pulls. And I wrote code for 199 00:13:13,940 --> 00:13:17,370 it on my flight here, so I have no 200 00:13:17,370 --> 00:13:20,160 idea whether that's correct or wrong, but like, we 201 00:13:20,160 --> 00:13:23,579 know it seems a little weird though. I've tried 202 00:13:23,579 --> 00:13:26,579 to compare if all the three pulls are equal 203 00:13:26,579 --> 00:13:31,730 and then I'm preparing some recipe with curry, and 204 00:13:31,730 --> 00:13:37,350 do you think it would work? It just looks 205 00:13:37,350 --> 00:13:40,810 a little bit of weird code, but I shall 206 00:13:40,810 --> 00:13:46,850 explain. So curry is a method in the proc 207 00:13:46,850 --> 00:13:51,769 class which actually returns a lambda if all the 208 00:13:51,769 --> 00:13:55,010 parameters are not fulfilled. So in this particular case, 209 00:13:55,010 --> 00:13:57,790 I needed three parameters, X, Y, and Z, and 210 00:13:57,790 --> 00:14:00,760 in the first statement, I passed only one. Note, 211 00:14:00,760 --> 00:14:04,040 the invocation is in square brackets. It's different. And 212 00:14:04,040 --> 00:14:06,720 it returns me a lambda, but other times when 213 00:14:06,720 --> 00:14:09,420 all the parameters are actually fulfilled in the second 214 00:14:09,420 --> 00:14:13,420 statement, it actually evaluates the proc. Now if you 215 00:14:13,420 --> 00:14:15,610 had to write the same code you would have 216 00:14:15,610 --> 00:14:18,459 taken different types of input, weighted for the user 217 00:14:18,459 --> 00:14:23,290 with (00:14:18:29) ...?? one, two, three, and evaluated it. 218 00:14:23,290 --> 00:14:24,920 You don't need to do that. And these things 219 00:14:24,920 --> 00:14:31,920 can actually be very helpful. So curry away. So! 220 00:14:34,300 --> 00:14:41,300 So you think you can tell protected from private. 221 00:14:41,949 --> 00:14:44,320 OK, I am not a - I'm a better 222 00:14:44,320 --> 00:14:50,360 programmer than a singer. Right. Private methods. All of 223 00:14:50,360 --> 00:14:54,000 us have been taught since school, unfortunately, that private 224 00:14:54,000 --> 00:14:58,740 methods are not inherited. Are they inherited? As you 225 00:14:58,740 --> 00:15:01,259 can see, foo is a private method. And I 226 00:15:01,259 --> 00:15:02,910 ran out of creativity, so I just went to 227 00:15:02,910 --> 00:15:06,060 the standard base type foo, blah blah blah here. 228 00:15:06,060 --> 00:15:10,620 Is this going to work? All of us take 229 00:15:10,620 --> 00:15:13,070 a lot of things for granted. How many of 230 00:15:13,070 --> 00:15:14,420 you all knew this already, that private methods are 231 00:15:14,420 --> 00:15:18,500 inherited in Ruby? All right, OK, now I'm scared. 232 00:15:18,500 --> 00:15:21,160 How many of you all didn't know this? Don't 233 00:15:21,160 --> 00:15:24,610 be shy, don't be shy. OK, so this is 234 00:15:24,610 --> 00:15:28,079 actually one of the basic principles of where Ruby 235 00:15:28,079 --> 00:15:32,339 actually breaks traditional object oriented concepts. All private methods 236 00:15:32,339 --> 00:15:36,699 are inherited. What? How many of you all have 237 00:15:36,699 --> 00:15:41,860 seen this code before? Forget the Mongoid::Document but include? 238 00:15:41,860 --> 00:15:46,029 Wait, come on. Everybody knows it! What is include? 239 00:15:46,029 --> 00:15:53,029 Is it a keyword? How many thought it was 240 00:15:53,139 --> 00:15:55,120 a keyword? Oh it's like that, require or that 241 00:15:55,120 --> 00:16:01,639 import in Java. Include is a private instance method 242 00:16:01,639 --> 00:16:07,500 defined in the class module. So while that is 243 00:16:07,500 --> 00:16:10,980 sinking in, it raises a basic question. All private 244 00:16:10,980 --> 00:16:17,980 methods are inherited. So what are protected methods? So 245 00:16:18,269 --> 00:16:19,889 what about protected methods? So what's the difference between 246 00:16:19,889 --> 00:16:23,089 private methods and protected methods now? We were told 247 00:16:23,089 --> 00:16:27,290 in our school that protected methods are inherited but 248 00:16:27,290 --> 00:16:32,110 not publicly accessible, right. Now private methods are inherited 249 00:16:32,110 --> 00:16:36,649 but not publicly accessible, right? So what are private 250 00:16:36,649 --> 00:16:41,130 metho- protected methods? Contrary to popular belief, in Ruby 251 00:16:41,130 --> 00:16:46,490 protected methods actually work with objects and not classes. 252 00:16:46,490 --> 00:16:49,610 And you can invoke a protected method on another 253 00:16:49,610 --> 00:16:54,339 object within the same lineage. And I say, what 254 00:16:54,339 --> 00:17:01,339 the... what? Better explained with an example. Simple example, 255 00:17:01,610 --> 00:17:03,060 for the sake of brevity I put the initializer 256 00:17:03,060 --> 00:17:09,230 in one line. What is the output, people? Don't 257 00:17:09,230 --> 00:17:15,040 be shy. You have bumblebee to help you out. 258 00:17:15,040 --> 00:17:19,990 No takers. Come on man, that's like pretty obvious 259 00:17:19,990 --> 00:17:23,050 right. You can't call a protected method on an 260 00:17:23,050 --> 00:17:30,050 object, right? Simple stuff. However. Now beginners, so-called beginners 261 00:17:31,340 --> 00:17:33,430 can tune out. This is fun. I added a 262 00:17:33,430 --> 00:17:38,740 method called fights. Same piece of code. What is 263 00:17:38,740 --> 00:17:44,070 the output? Take a wild guess, man. Go for 264 00:17:44,070 --> 00:17:51,070 it. So either Megatron or Decepticon. Any other deceptive 265 00:17:52,350 --> 00:17:59,350 ideas? Guys, very practically, a quick lesson in human 266 00:17:59,880 --> 00:18:02,710 psychology. If that didn't work, would I have that 267 00:18:02,710 --> 00:18:08,150 slide up there again? It works, man. It works. 268 00:18:08,150 --> 00:18:09,940 But why did it work and how did it 269 00:18:09,940 --> 00:18:13,430 work? The interesting part is that piece of line, 270 00:18:13,430 --> 00:18:15,660 earlier in the previous slide, prime dot nick did 271 00:18:15,660 --> 00:18:18,550 not work, but here target dot nick actually works. 272 00:18:18,550 --> 00:18:22,120 I have called a protected method on an object. 273 00:18:22,120 --> 00:18:25,960 Now that same lineage means that because I'm inside 274 00:18:25,960 --> 00:18:28,950 the class Autobot, because I'm calling a method on 275 00:18:28,950 --> 00:18:32,320 the prime object, which is in, which is in 276 00:18:32,320 --> 00:18:36,190 Autobot class, and since another object called Megatron there 277 00:18:36,190 --> 00:18:40,810 is of the same lineage, it's also an Autobot. 278 00:18:40,810 --> 00:18:46,850 I can call the protected methods. So why didn't 279 00:18:46,850 --> 00:18:49,900 it work earlier? Because everything in Ruby is an 280 00:18:49,900 --> 00:18:52,340 object. If you're trying to start prime dot nick 281 00:18:52,340 --> 00:18:55,140 and the previous case was outside the scope. So 282 00:18:55,140 --> 00:18:57,640 it was effectively in some main class, not the 283 00:18:57,640 --> 00:19:00,730 same lineage, so it doesn't work. So Ruby actually 284 00:19:00,730 --> 00:19:04,400 works in the traditional way, but has different internal 285 00:19:04,400 --> 00:19:07,380 meanings. And if now this was getting a little 286 00:19:07,380 --> 00:19:10,430 complicated, how many of you all thought there are 287 00:19:10,430 --> 00:19:12,430 keywords in Ruby? Cause that man there is gonna 288 00:19:12,430 --> 00:19:14,260 come and shoot you if you think Ruby has 289 00:19:14,260 --> 00:19:21,260 keywords. Does that code work? Is it even valid? 290 00:19:23,700 --> 00:19:29,640 Should it give me a syntax error? If, how 291 00:19:29,640 --> 00:19:30,990 many of you think it will give me an 292 00:19:30,990 --> 00:19:33,460 error? Syntax error too? What's wrong with you? Have 293 00:19:33,460 --> 00:19:37,070 you gone mad?-kind of errors. Any takers for syntax 294 00:19:37,070 --> 00:19:40,010 errors? Probably not. But if not, then I want 295 00:19:40,010 --> 00:19:41,280 to ask you know what is the output of 296 00:19:41,280 --> 00:19:48,280 this code? Seriously. There it is, seriously. Question is, 297 00:19:50,440 --> 00:19:51,710 how many of you all thought this is an 298 00:19:51,710 --> 00:19:55,400 error called stack_too_deep. Cause when I call false it 299 00:19:55,400 --> 00:19:57,770 calls true, once you call true it will call 300 00:19:57,770 --> 00:20:02,250 false. And it goes into a recursive loop and 301 00:20:02,250 --> 00:20:08,330 case stack_too_deep. Any takers for stack_too_deep? Higher, higher! And 302 00:20:08,330 --> 00:20:12,240 how many think it will actually work? All right. 303 00:20:12,240 --> 00:20:15,510 The rest of the people, you all are beginners. 304 00:20:15,510 --> 00:20:17,300 But yeah this works. So it does not stack_too_deep. 305 00:20:17,300 --> 00:20:21,680 This actually works because true, true's resolution is immediately 306 00:20:21,680 --> 00:20:24,800 determined as the boolean value. However if I had 307 00:20:24,800 --> 00:20:29,650 changed these two particular statements, the content in the 308 00:20:29,650 --> 00:20:32,130 methods to self dot false and self dot true 309 00:20:32,130 --> 00:20:39,130 I would probably get stack_too_deep. Not probably, definitely get 310 00:20:39,190 --> 00:20:42,270 stack_too_deep. OK. So. If this wasn't complex enough for 311 00:20:42,270 --> 00:20:49,200 you, let's go to modules, the mysterious modules. You 312 00:20:49,200 --> 00:20:51,030 know what the bull I have with my good 313 00:20:51,030 --> 00:20:54,660 friend module, which has power, Megatron is super powerful, 314 00:20:54,660 --> 00:20:57,990 and he's a force, he's evil. And I want 315 00:20:57,990 --> 00:21:00,910 to put that in my class Hanuman. note the 316 00:21:00,910 --> 00:21:04,070 star, because that's true. I want the power of 317 00:21:04,070 --> 00:21:10,590 Megatron with Hanuman. Hanuman! Unfortunately this will not go 318 00:21:10,590 --> 00:21:13,460 down too well religiously for us, because though it 319 00:21:13,460 --> 00:21:15,890 will be Hanuman will be as powerful as Megatron, 320 00:21:15,890 --> 00:21:19,960 Hanuman is now evil! So what do I do? 321 00:21:19,960 --> 00:21:22,560 Is there a way I can cherry-pick from Megatron, 322 00:21:22,560 --> 00:21:25,650 saying, I want the power of Megatron but I 323 00:21:25,650 --> 00:21:32,650 don't want to be evil? What do I do? 324 00:21:33,060 --> 00:21:35,700 I require. Note, I have not included Megatron, I 325 00:21:35,700 --> 00:21:39,980 require the Megatron method- Megatron file, Megatron RB. I 326 00:21:39,980 --> 00:21:43,270 define a method called power and I go to 327 00:21:43,270 --> 00:21:46,190 the module, tell him to give me that instance 328 00:21:46,190 --> 00:21:49,600 method called power, which gives me something called an 329 00:21:49,600 --> 00:21:54,030 unbounded method. And then I bind it to me, 330 00:21:54,030 --> 00:21:59,730 that is self, and I call it. And that 331 00:21:59,730 --> 00:22:06,730 makes my Hanuman religiously acceptable to society. Question? No. 332 00:22:09,150 --> 00:22:11,870 So note self, self is the object call. So 333 00:22:11,870 --> 00:22:13,680 it's not working on the class call, it's actually 334 00:22:13,680 --> 00:22:16,260 working on the instance. So every object, every instance 335 00:22:16,260 --> 00:22:21,660 of Hanuman, will actually get the power of Megatron. 336 00:22:21,660 --> 00:22:26,890 And with that I end my talk and thank 337 00:22:26,890 --> 00:22:29,980 my two assistants here. I have, I'm open for 338 00:22:29,980 --> 00:22:33,820 a few questions. Do I have the time, guys? 339 00:22:33,820 --> 00:22:35,910 Satish, do I have some time? For questions? Yay! 340 00:22:35,910 --> 00:22:40,650 We have a lot of time for questions! All 341 00:22:40,650 --> 00:22:47,650 right, you. Thank you. You have a question there. 342 00:22:49,190 --> 00:22:52,970 QUESTION: What exactly lead you to title this talk 343 00:22:52,970 --> 00:22:56,840 as the Dark Side of Ruby? G.R.: Oooh. Now 344 00:22:56,840 --> 00:22:59,660 he killed me, didn't he? So that was a, 345 00:22:59,660 --> 00:23:01,510 there's a two-sided answer to that. One was to 346 00:23:01,510 --> 00:23:06,020 fool the organizers, to get the CF?? (00:23:05:23) accepted. 347 00:23:06,020 --> 00:23:10,510 But more importantly, the dark side of Ruby tries 348 00:23:10,510 --> 00:23:12,130 to bring out the weirdness in Ruby, which is 349 00:23:12,130 --> 00:23:15,700 not the evil part, but the hidden part. Like 350 00:23:15,700 --> 00:23:18,130 the dark side of the moon where we have 351 00:23:18,130 --> 00:23:20,180 all these kind of stuff that breaks, the gotchas, 352 00:23:20,180 --> 00:23:22,870 the moment that we feel that are going to 353 00:23:22,870 --> 00:23:25,740 help but you don't really need to care about 354 00:23:25,740 --> 00:23:28,160 it, but it's really, really important that you know 355 00:23:28,160 --> 00:23:32,840 these things exist. That's why Dark Side of Ruby. 356 00:23:32,840 --> 00:23:37,920 So I still love Ruby - no offense. Any 357 00:23:37,920 --> 00:23:44,920 other questions? Yes sir. QUESTION: So have you suffered 358 00:23:45,120 --> 00:23:47,040 because of any of these gotchas in you work? 359 00:23:47,040 --> 00:23:50,940 G.R.: Absolutely! This is all part of experience. Part 360 00:23:50,940 --> 00:23:55,860 (00:23:53:00 ??) is part of experience, part is training, 361 00:23:55,860 --> 00:23:59,310 and partly is preparation for my talk, trying to 362 00:23:59,310 --> 00:24:02,030 find out these terms, and figuring things out. There's 363 00:24:02,030 --> 00:24:04,170 a lot of stuff that I've actually kept out, 364 00:24:04,170 --> 00:24:08,370 like you know ?? (00:24:07:03) games. Lots of fun 365 00:24:08,370 --> 00:24:10,850 with method missing. I haven't even touched upon blocks 366 00:24:10,850 --> 00:24:12,970 and ?? There's lots of funny stuff which happens 367 00:24:12,970 --> 00:24:17,130 there. But I have only half an hour. So 368 00:24:17,130 --> 00:24:18,450 the question to you guys also is that in 369 00:24:18,450 --> 00:24:20,260 case you find some of these funny things which 370 00:24:20,260 --> 00:24:23,040 happen in Ruby, and are able to explain it 371 00:24:23,040 --> 00:24:24,770 or not able to explain it, send me a 372 00:24:24,770 --> 00:24:27,580 Tweet, send me an email, and I will probably 373 00:24:27,580 --> 00:24:30,600 investigate and improve my talk at your expense. QUESTION: 374 00:24:30,600 --> 00:24:35,380 Yeah. So the other question is, are there some 375 00:24:35,380 --> 00:24:39,850 practices, certain things that I follow, so that I 376 00:24:39,850 --> 00:24:42,730 don't get, I don't encounter these? G.R.: Well, so 377 00:24:42,730 --> 00:24:44,220 one thing, these aren't problems. What I wanted to 378 00:24:44,220 --> 00:24:45,900 point out here is that this is not a 379 00:24:45,900 --> 00:24:47,390 bad part of Ruby, but these are the things 380 00:24:47,390 --> 00:24:50,130 that we take for granted. So knowing about protected 381 00:24:50,130 --> 00:24:55,050 and private being different is important. But not mandatory 382 00:24:55,050 --> 00:24:57,910 for us to work as Rails programmers. But if 383 00:24:57,910 --> 00:25:01,750 you know that, how protected and private actually work, 384 00:25:01,750 --> 00:25:05,050 it makes us better programmers. So best practices of 385 00:25:05,050 --> 00:25:07,450 Ruby are already there. You know there are things 386 00:25:07,450 --> 00:25:11,380 like flip-flops in Ruby which is for lack of 387 00:25:11,380 --> 00:25:15,220 a better term, a mind star star star star. 388 00:25:15,220 --> 00:25:18,010 But, I do not recommend it. But it's there. 389 00:25:18,010 --> 00:25:20,860 There are plenty of other things in Ruby, too. 390 00:25:20,860 --> 00:25:24,620 Which work, which should work, which you should use. 391 00:25:24,620 --> 00:25:26,970 The bottom line is that Ruby gives us the 392 00:25:26,970 --> 00:25:29,530 maximum flexibility to build the way we want to 393 00:25:29,530 --> 00:25:33,050 build our code. So with great power comes great 394 00:25:33,050 --> 00:25:35,530 responsibility. And sometimes it's irresponsible. So it worked out 395 00:25:35,530 --> 00:25:39,040 great. Yeah. QUESTION: So is there a good parts 396 00:25:39,040 --> 00:25:41,460 of Ruby, like good parts of Java- G.R.: What? 397 00:25:41,460 --> 00:25:43,890 QUESTION: Good parts of Ruby? G.R.: Oh, good parts? 398 00:25:43,890 --> 00:25:45,650 Everything! I've been working in Ruby for six years 399 00:25:45,650 --> 00:25:48,050 and I still love it. QUESTION: No, there is 400 00:25:48,050 --> 00:25:49,320 a famous book called Good Parts of JavaScript, a 401 00:25:49,320 --> 00:25:51,600 very thin book that allows a subset of language 402 00:25:51,600 --> 00:25:55,600 that is very safe to use, that you can 403 00:25:55,600 --> 00:26:02,110 be very productive in. So do you, have you 404 00:26:02,110 --> 00:26:02,200 come across something that says, like, there's a subset 405 00:26:02,200 --> 00:26:02,300 that you should stick to and use? G.R.: So 406 00:26:02,300 --> 00:26:05,390 the best part about Ruby that I like is 407 00:26:05,390 --> 00:26:09,070 the closures, which is completely misunderstood, because every time 408 00:26:09,070 --> 00:26:11,390 I talk to any person who's getting into Ruby 409 00:26:11,390 --> 00:26:12,620 and I show them what a closure is they're 410 00:26:12,620 --> 00:26:15,060 like yeah, that's a loop. But it's not. It's 411 00:26:15,060 --> 00:26:17,670 like far more better than that. And it's far 412 00:26:17,670 --> 00:26:20,660 more - it's more intuitive. It gets into the 413 00:26:20,660 --> 00:26:23,410 details and gives us so much more incentive about 414 00:26:23,410 --> 00:26:26,160 learning how Ruby internals work, that we tend to 415 00:26:26,160 --> 00:26:29,100 actually ignore it in Rails. And I only (00:26:30:29) 416 00:26:29,100 --> 00:26:31,530 have a lot of respect for people who have 417 00:26:31,530 --> 00:26:33,010 learned Ruby and gone to Rails, because they know 418 00:26:33,010 --> 00:26:35,790 the right thing, other than most of us, including 419 00:26:35,790 --> 00:26:38,340 me, who got into Rails and then fell in 420 00:26:38,340 --> 00:26:41,770 love with Ruby. So it's different. QUESTION: So is 421 00:26:41,770 --> 00:26:46,310 Ruby the good parts the next book? G.R.: Could 422 00:26:46,310 --> 00:26:48,090 be. But I'll need a lot of community support 423 00:26:48,090 --> 00:26:54,900 for that. Yes? QUESTION: Yeah, hi, thanks for the 424 00:26:54,900 --> 00:26:57,390 talk. Just (00:26:55:09). I have been checking out Exorcism 425 00:26:57,390 --> 00:26:59,030 dot IO. I don't know if you guys have 426 00:26:59,030 --> 00:27:01,750 heard of it. So it's actually started by Katrina, 427 00:27:01,750 --> 00:27:04,000 who's part of the Ruby Rogues, and I have 428 00:27:04,000 --> 00:27:06,530 had a wonderful time running stuff from Ruby, like 429 00:27:06,530 --> 00:27:08,940 I have been working Ruby for ?? (00:27:08:20) for 430 00:27:08,940 --> 00:27:11,650 years and also gotchas- G.R.: So, Exorcism dot IO. 431 00:27:11,650 --> 00:27:14,560 That's great. QUESTION: Like really good Ruby idioms that 432 00:27:14,560 --> 00:27:15,500 like so it's basically what happens is you, there 433 00:27:15,500 --> 00:27:17,210 are around ten languages if I'm not wrong. You 434 00:27:17,210 --> 00:27:21,160 can pick Ruby, you can sub in your solutions. 435 00:27:21,160 --> 00:27:23,660 People can look at your code. They can give 436 00:27:23,660 --> 00:27:26,810 good reviews. You can help out others with your 437 00:27:26,810 --> 00:27:30,190 suggestions. Yeah- G.R.: But to add to that, there 438 00:27:30,190 --> 00:27:32,370 is also some interesting - I'm not sure if 439 00:27:32,370 --> 00:27:35,870 you all have heard of Winbgall (00:27:33:08). Wingolf was 440 00:27:35,870 --> 00:27:38,670 a gem and a simple tool where you can 441 00:27:38,670 --> 00:27:42,150 report a problem statement, and using whim, you know 442 00:27:42,150 --> 00:27:46,150 execute that in the least number of key presses. 443 00:27:46,150 --> 00:27:48,650 Similarly there are a few initiatives for Ruby golf, 444 00:27:48,650 --> 00:27:51,350 where you can actually try things like, the beginner 445 00:27:51,350 --> 00:27:54,210 ?? they actually said they have things with ?? 446 00:27:54,210 --> 00:27:57,790 you know stuff like, write this piece of code 447 00:27:57,790 --> 00:28:01,130 without using arrays, or without using the equality operator. 448 00:28:01,130 --> 00:28:03,500 And then you start, your mind starts churning, and 449 00:28:03,500 --> 00:28:05,090 then you dig deeper into Ruby and find other 450 00:28:05,090 --> 00:28:07,140 things. So the best way to find these things 451 00:28:07,140 --> 00:28:10,850 is to try Ruby ?? All right we had 452 00:28:10,850 --> 00:28:13,380 a question there too. QUESTION: Not really a question, 453 00:28:13,380 --> 00:28:16,210 but just a suggestion, actually, to folks who are 454 00:28:16,210 --> 00:28:18,740 trying to learn Ruby. I like that he pointed 455 00:28:18,740 --> 00:28:20,700 out that some of the things that we've learned 456 00:28:20,700 --> 00:28:24,290 in college, classical logic and stuff doesn't really apply. 457 00:28:24,290 --> 00:28:27,750 What really helped me understand differences was that treat 458 00:28:27,750 --> 00:28:30,840 Ruby as a object-focused language and most of the 459 00:28:30,840 --> 00:28:33,120 languages that you probably end up working with, Java, 460 00:28:33,120 --> 00:28:36,280 C sharp, they'll be class focused. So concepts like 461 00:28:36,280 --> 00:28:39,340 private, protected, just like you mentioned, are all object-level 462 00:28:39,340 --> 00:28:41,580 concepts in Ruby. In fact if you wanted to 463 00:28:41,580 --> 00:28:44,570 draw parallels again JavaScript is a good parallel because 464 00:28:44,570 --> 00:28:47,230 JavaScript is also an object-focused language. So there are a 465 00:28:47,230 --> 00:28:48,950 lot of parallels between JavaScript and Ruby, and 466 00:28:49,000 --> 00:28:51,700 less between Java and Ruby. 467 00:28:51,700 --> 00:28:52,700 QUERANT: Yeah, and- 468 00:28:52,740 --> 00:28:55,700 It's like the dot, I'm sorry, that dot thing just amazed me. 469 00:28:55,700 --> 00:28:58,780 ??, just having a dot. 470 00:28:58,900 --> 00:29:01,460 and I was actually kind of disappointed when it 471 00:29:01,460 --> 00:29:04,040 ended. I just wanted more. OK. 472 00:29:04,140 --> 00:29:05,140 G.R.: Cool. Thank you. 473 00:29:08,260 --> 00:29:09,260 Yes? 474 00:29:11,080 --> 00:29:13,200 The Well-Grounded Rubyist. A must read. 475 00:29:14,260 --> 00:29:15,680 V.O.: Somebody Tweet about it, please. 476 00:29:16,340 --> 00:29:17,680 V.O.: We'll take one last question. 477 00:29:19,500 --> 00:29:21,840 QUERANT: How does it help to have protected and private method 478 00:29:21,940 --> 00:29:24,940 different from other languages? The way Ruby does it? 479 00:29:25,160 --> 00:29:27,780 G.R.: I'm sorry. Did you ask what is different between protected and private- 480 00:29:27,780 --> 00:29:30,140 QUERANT: No. How does it help to have a different design implementation, 481 00:29:30,140 --> 00:29:31,140 to have it different. 482 00:29:31,140 --> 00:29:33,720 G.R.: Oh. Excellent. And I love answering this question. 483 00:29:33,720 --> 00:29:35,560 Though sometimes it can be a little long. 484 00:29:35,560 --> 00:29:39,760 Why is private and protected different private and protected different in Ruby than 485 00:29:39,760 --> 00:29:41,540 in other traditional standard languages? 486 00:29:41,540 --> 00:29:43,160 Because Ruby did it right. 487 00:29:43,160 --> 00:29:44,840 That's how it's supposed to be. 488 00:29:44,840 --> 00:29:46,240 Example. 489 00:29:48,180 --> 00:29:50,440 You know, when we talk about object-oriented programming, 490 00:29:50,440 --> 00:29:52,680 it's supposed to be about objects. Things that you can see and touch. 491 00:29:52,680 --> 00:29:55,060 For example: Inheritance. 492 00:29:55,060 --> 00:29:57,020 Right. What is inheritance? 493 00:29:57,020 --> 00:29:59,380 In, put it in, no uncertain term, though, it's my 494 00:29:59,380 --> 00:30:01,080 ?? 495 00:30:01,820 --> 00:30:04,900 Right. My inherited land my from my ancestors. 496 00:30:06,500 --> 00:30:08,740 It's private. It's inherited. 497 00:30:08,740 --> 00:30:11,620 You can. It, it comes within the family. 498 00:30:11,620 --> 00:30:13,640 It can be protected. Can be private. 499 00:30:13,640 --> 00:30:16,900 But, if you have your dad's wallet, do you inherit it? 500 00:30:18,280 --> 00:30:20,440 You can choose to use it. You can choose not to use it. 501 00:30:20,500 --> 00:30:21,620 It's just there. 502 00:30:21,620 --> 00:30:24,980 The inheritance of land can be protected because it always stays in the family. 503 00:30:24,980 --> 00:30:28,440 Like, for example, the money in the family is protected. 504 00:30:28,440 --> 00:30:31,340 But that's always dealing with parents and children. 505 00:30:31,340 --> 00:30:35,460 You can't go, your father. Just because you have a father and I have a father 506 00:30:35,460 --> 00:30:37,000 I can't go asking your father for money. 507 00:30:37,000 --> 00:30:40,700 Right. It's immense, how you determine. And of course public is the same everywhere. 508 00:30:41,260 --> 00:30:43,960 So Ruby did it right, because it's, everything's object-centric. 509 00:30:44,680 --> 00:30:47,940 Exactly what Aman said just now. Everything's object centric. 510 00:30:48,660 --> 00:30:52,140 And, traditionally in object-oriented concepts, it started off right 511 00:30:52,140 --> 00:30:57,020 and then it went all across the tangent, where you had this inheritance, and 512 00:30:57,020 --> 00:30:59,120 classes and stuff like that. All the standard 513 00:31:00,060 --> 00:31:06,480 things that I never talk about: incapsulation, abstraction, polymorphism 514 00:31:06,480 --> 00:31:09,440 which seem to be the essence of object-oriented programming but is not. 515 00:31:10,240 --> 00:31:11,840 V.O.: So, a follow-up question. 516 00:31:11,840 --> 00:31:16,380 I have it. So is there a well-known pattern that makes use of, makes good of 517 00:31:16,380 --> 00:31:18,200 private and protected? 518 00:31:18,980 --> 00:31:19,980 We understand why- G.R.: Good qu- 519 00:31:19,980 --> 00:31:20,980 QUERANT: Is it because it 520 00:31:20,980 --> 00:31:24,400 G.R.: Include. Include. Include is an excellent example. 521 00:31:24,400 --> 00:31:26,660 There are actually other patterns, too. 522 00:31:26,660 --> 00:31:30,780 There are other patterns which allow for protected, protected is used very heavily for 523 00:31:30,780 --> 00:31:34,080 all sorts of, the usually standard, why use protected, for example 524 00:31:34,080 --> 00:31:37,180 protecting your password generation algorithm. 525 00:31:37,180 --> 00:31:39,060 Something like that. But that's the deal with objects. 526 00:31:39,180 --> 00:31:41,980 And since objects can be called only from their own lineage 527 00:31:41,980 --> 00:31:43,940 you're always safe. Good question. 528 00:31:46,000 --> 00:31:47,460 All right. V.O.: Anymore questions? 529 00:31:48,380 --> 00:31:50,340 If there's anymore questions, we'll take it offline. 530 00:31:50,340 --> 00:31:52,140 G.R.: Sure. Thank you so much 531 00:31:53,840 --> 00:31:54,840 V.O.: Thanks Gautam.