Ruby on Rails

Who Wants to be a Ruby Engineer?

Who Wants to be a Ruby Engineer?

by Drew Bragg

In the video titled "Who Wants to be a Ruby Engineer?" hosted by Drew Bragg at RailsConf 2023, the audience participates in an engaging Ruby code game show. The main theme revolves around analyzing and guessing the output of unconventional snippets of Ruby code, challenging common knowledge of the language and reinforcing the idea that Ruby, despite its quirks, should remain understandable and maintainable.

Key points discussed include:

- Game Format: Contestants take turns answering multiple-choice questions related to Ruby code snippets, using audience participation for assistance. They have powers such as pairing with an audience member or ‘searching’ Stack Overflow for help.
- Emphasis on Learning: Incorrect answers are celebrated as learning opportunities rather than failures. This highlights the importance of curiosity and exploration in programming.

- Complexity of Ruby: Drew emphasizes the uniqueness and whimsicality of Ruby, demonstrating various obscure and less conventional syntax and behaviors in the language with examples.
- Examples of Ruby Syntax: Throughout the contest, several examples are shared, such as using double colons for method resolution, manipulating ranges with parentheses, handling hashes, and exploring the behavior of procs. Each round illustrates a specific concept or potential pitfall in Ruby.

- Significant Lessons: Key takeaways include the necessity for clear and readable code, the understanding of local vs. global scopes, the importance of parentheses in method calls, and the peculiarities of Ruby's syntax that can lead to unexpected results.

- Final Thoughts: Drew urges rubyists to keep their code whimsical but also readable, summarizing the affection many have for Ruby despite its complexity.

The session wraps up by thanking participants and inviting further questions, fostering a sense of community among Ruby developers.

00:00:20.060 And welcome to 'Who Wants to be a Ruby Engineer?' This is a Ruby game show, and I am your host. Look, that's me! Sorry.
00:00:27.539 My name is Drew Bragg. Contrary to popular belief, I am from a city called Philadelphia, not a state called Oklahoma. I am a Staff Engineer at a lovely company called Within3. I also host a podcast called 'Code and The Code Encoders.' If you can't tell, I'm not great at coming up with good names.
00:00:41.160 I also co-organize the monthly Meetup for Philly RB, which is a Philadelphia Ruby Meetup. You do not have to be from Philadelphia to join us, as it's all virtual now. Just come and hang out with other cool Rubyists. I can be found on the internet most places as Dr. Bragg.
00:00:58.320 Now that the not-so-fun stuff is out of the way, I want to say first that most of the Ruby syntax you're about to see is valid Ruby. It'll run, but I don't recommend putting it in production. You can use it to confuse your co-workers or play practical jokes on them—it's a lot of fun! You can also use it to gain a better understanding of how Ruby works. Just remember, don’t put it in production; it's not a good idea.
00:01:13.080 I love that Ruby is a weird language. I love that we can be expressive with it. I think it's great to keep Ruby weird. I like fun Ruby; it's so whimsical, and there are so many cool things we can do with it. However, we are professionals, and we owe a debt to our future selves and our co-workers to keep Ruby readable.
00:01:37.740 I'm going to try and drive home that point today in a fun way. We're going to play a game called 'Who Wants to be a Ruby Engineer?' I have some lovely contestants here up in the front row, and they're going to get up one at a time to guess the output of a small snippet of Ruby.
00:01:55.680 I am not a monster; it will be multiple choice. They'll have A, B, C, or D to choose from. If they're still stuck, they have two powers they can use: they can pair program, which means they can select someone from the audience to give them a hand, or they can search Stack Overflow. Spoiler alert: you're all Stack Overflow; we'll take a poll and go with whatever gets the most votes.
00:02:18.700 If a contestant gets a question wrong, they will sit down. Contestants probably will get them wrong, as I went out of my way to make these kind of hard. We should applaud them when they get a question wrong because that is not a failure of knowledge; that is a success in learning. This is weird Ruby for a reason.
00:02:37.180 But it is a game show, so I do have some awesome prizes thanks to some wonderful people. Brittany Martin from the Ruby on Rails podcast donated a bunch of cool stuff. If you don't listen to the podcast, you absolutely should get on that. The one and only Andrew Mason contributed some money and let me go on a spending spree. You probably know him from Remote Ruby or Ruby for All or the Ruby Road Radar newsletter, because he does all the things.
00:02:55.440 The one and only Andy Kroll donated some wise and poignant guides to Ruby, which is an awesome book.
00:03:01.740 So, are you guys ready? I'm ready; I think I'm ready. I don't know; we'll find out. Alright, so let's get our first contestant. Come on down! Anyone who wants to volunteer to go first?
00:03:22.440 My name is Rock Davenport. I'm from Charlotte, North Carolina, by way of Brooklyn, New York. Yeah, here we go! And I've been working with Ruby for two years.
00:03:37.920 Excellent! Alright, are you ready to play? No? Okay, well, we're going to do it anyway.
00:03:44.100 I will give you the first question. It's going to be a warm-up question so you know how this works and so that everybody else can get a feel for how this is going to go. If you get it right, great! Bonus points for you. If you get it wrong, you still stay up, and we'll do a real question. Sound good? Fair enough? Excellent!
00:03:59.540 So your first question is, if you take a look up at the screen, let me get to the question. Alright, so for folks in the back, this is the size of my Ruby syntax; you may want to move up or over here, as it's a little easier to see.
00:04:14.520 So, we're going to have one with two colons, a plus sign, some parentheses with a two inside. What are we going to get? Is it going to be A) 3, B) a NoMethodError, C) 12, or D) a syntax error?
00:04:37.700 A? Excellent! Nice work. First question down!
00:04:46.220 Now, for those of you who are thinking, 'Wait, what? How did that give us 3?' You’re probably familiar with the double colon as a namespace resolution operator. It's how we move through constants and do constant lookups and things of that nature. But it can also be used to call instance methods.
00:05:07.380 Since 1 is an instance of an integer, we can just call plus on it and give it an argument of 2. The double colon plus is equivalent to sending the symbol plus or, more commonly, 1 plus 2. Yeah, for the love of math, please use 1 plus 2! Don't do this.
00:05:20.160 Don't just don't do that. And definitely don't put it in production. Alright, are you ready for a real question?
00:05:38.040 You’re great! Alright, so here we go. Take a look up at the screen. We have: 1.a.a 5.a 2.a. There’s an underscore in there. You ready for your answers? Yes? Got it!
00:05:56.940 A) a Range, B) a NoMethodError, C) nil, or D) an array with 1, 2, 3, 4, and 5.
00:06:06.760 Now remember, you have the powers. You can pair; you can search Stack Overflow.
00:06:19.600 Stack Overflow? Alright, what do we think crowd? Are we going to go with A) a Range? Hands up.
00:06:28.780 A couple of people. B) a NoMethodError—okay, more people. C) nil—yeah, a handful. Or D) the array. Whoa, that's a lot of people who don't know what they're doing. Wow!
00:06:38.979 What do you think?
00:06:51.860 I mean, most people said D. I'm going to go with B.
00:07:03.960 Smart man, smart man. Sometimes Stack Overflow is wrong.
00:07:14.619 So for those who voted for D and are confused, you need to wrap ranges in parentheses in order to call methods on them because of how operator precedence works. What we just saw was equivalent to: 1..5 to A.
00:07:27.360 And integers don’t have a 2.A method on them, so don’t forget your parentheses. Nice work! I got lucky; that's a good one!
00:07:39.920 Alright, let's try something a little different. We have a global variable called hello. We're going to set it to the string 'world.' Then we are going to use hello with a pound sign and a dollar sign and 'hello.' So what are we going to get here?
00:07:57.620 Are we going to get A) a syntax error, B) the string 'hello world,' C) the literal string up there 'hello pound dollar sign hello,' or D) a name error?
00:08:06.840 C? You still have one more power; I know.
00:08:28.159 Unfortunately, no! I discovered this very fun thing with a typo and was very confused as to how things were still working. So it turns out when you're interpolating instance variables—the ones with the @—class variables, the two @’s, or global variables—you don’t need the curly braces.
00:08:46.200 You can just use the pound sign. So round of applause for Rock! Thank you for playing!
00:09:03.300 Alright, I'm going to need my next contestant. Come on down!
00:09:32.920 My name is Dominic Rodriguez. I am from San Diego, Dominican Republic, and I've been working with Ruby for two years.
00:09:50.760 Great! Alright, you ready to play? No? Oh well; neither was Rock, and he did very well! So let's see how you do with this guy.
00:10:01.660 We have a method called stringify. I think that's stringify—yes, it is! And it takes an argument called hash. Then we're doing it. If we have hash, we'll do hash.to_s. So when we call stringify and pass it curly braces, what do we get?
00:10:21.170 Do we get A) the string of an empty hash (just two curly braces), B) nil, C) an argument error, or D) a NoMethodError?
00:10:35.760 And remember, you do have your power-ups, so you can ask Stack Overflow, although they've proven to be not great. You should have gone with ChatGPT!
00:10:46.620 Or you can pick someone out of the crowd to help you out. So what do you think? I think A.
00:11:01.580 Are you sure you want to use power-ups? No? Sure!
00:11:14.850 No, it's an ArgumentError, unfortunately. So for those who thought it was A, thank you.
00:11:21.920 You probably know that we can omit parentheses when we're calling methods and passing some variables; however, you have to be careful because when dealing with hashes like this, Ruby's going to interpret it as a block.
00:11:39.159 So to ensure that the method is passed as a hash, you need the parentheses. A little bit of a theme there, I think.
00:11:50.640 Alright, that means I need my next contestant. Come on down!
00:11:57.640 Alright, same deal. I'm Christine. I'm from Queens, New York, and I've been working with Ruby for about eight years, but I don't think that's going to help me here.
00:12:14.360 Um, yeah, well, let’s find out. Here’s your first question: oh, syntax highlighting! Why do you never work for me? We have a variable Foo, and it's set to the string 'bar' because I, again, don't do well with naming.
00:12:30.760 We have some regex stuff in an equal with the tilde and the string 'bar' again. So what’s Foo? Is it the string 'bar,' a NameError, the string 'b,' or a syntax error?
00:12:55.660 And you do have your power-ups! Should have downloaded the Jeopardy theme.
00:13:09.520 Yeah, what's print? What's print, Foo? Yeah, okay.
00:13:22.140 Um... pairing a random guess? I’m close to just giving a random guess.
00:13:50.680 Yes? What do you think? You think it's C? Okay, because that’s what I was thinking based on the /w but capture. Yeah, okay.
00:14:06.920 Alright, let’s see. Nice work!
00:14:18.080 Alright, so when we are doing regular expressions with Ruby, by using named captures and our equals sign with the tilde, we're going to create local variables for those named groups.
00:14:34.360 If we already have variables with those names, we're going to override them. We need to be careful; I've gotten bitten by this.
00:14:46.600 Alright, are you ready for your next one? Here’s an easy one for you! Move the range 1 through 5, and we're going to iterate over it. Next, if I double equals 2... dot dot I equals 4; otherwise, print I. So, are we going to get 2, 3, and 4, 3, 1, and 5, or a syntax error for using too many dots?
00:15:15.360 You can’t pair, but you can use Stack Overflow. Okay, so if on the cases where in the range, next if there’s a range true or false to...
00:15:30.880 Well, you’re never going to have both universe; I is ever going to be able to equal. Thinking the word false or false or true and false but never true and true.
00:15:45.760 And then it’s put, saying right, so that can only happen. This is great; I like hearing you think out loud. This is awesome! Most people just stand up here silently going, 'What is wrong with you?'
00:16:00.580 My brain! Same! Which is why this is in here.
00:16:22.400 Why Stack Overflow?
00:16:35.560 Alright, Stack Overflow; what do we think? A) 2, 3, 4? Anybody? Bueller?
00:16:51.280 A couple of people. B) just 3? Nobody.
00:17:03.900 C) 1 and 5? A couple people. Or D) a syntax error?
00:17:11.780 C or D? Yes, yes, flip a coin! Um, I guess the copy-paste I’ll be doing from Stack Overflow is C: 1 and 5.
00:17:36.200 So for those of you who want to Google this, this is called a flip-flop operator, and it hurts my head! Basically, how a flip-flop operator works is when you have a conditional inside of a loop, it will evaluate to true when the first thing evaluates to true and until the last thing evaluates to true.
00:17:49.240 So we're just going to skip 2, 3, and 4, which is why we get 1 and 5. Fun fact: this was deprecated for a while, and then they opted to keep it. You’re doing good! Are you ready for another one?
00:18:03.450 The syntax highlighting does not cooperate. Alright, sorry again about syntax highlighting because it works and then it doesn't.
00:18:09.360 So we have a class Boop because I’m getting better with naming, and we're going to define an instance method of bang (or exclamation, whatever you call it) and that’s going to return 'beep'.
00:18:21.080 Because naming! So, not boop.new.
00:18:29.400 A) false, B) true, C) beep, D) no method error?
00:18:38.490 No power-ups, though; yeah, you’re all out. No, it's all alone!
00:18:50.160 Andy, I should give you a mic and have you do the music. No one wants to hear me sing! Um... I will go with A) false.
00:19:03.680 Actually, it’s going to return B) beep. Yes!
00:19:17.620 The people who were like 'Wait, what?' You’re right. This is interesting. So when you call the not keyword, what it actually does is it calls the bang method on the thing you’re calling it on.
00:19:32.580 In this case, we overrode it, so we get 'beep.' Alright, so that means I need another contestant! Thomas!
00:20:04.240 Yeah, my name is Thomas; I'm from Copenhagen by way of Queens. I've never written Ruby before, so I probably won’t get these, and that’s the reason why.
00:20:13.640 Well, that’s fun! So let me introduce you to my friend called Ruby. That’s a really nice programming language.
00:20:20.560 No, seriously, yeah. No, I've been writing... This is a bad example of intuitive Ruby; this is not how you should ingest Ruby the first time.
00:20:31.310 How long have you actually been doing Ruby? Oh, since 2017. Okay, so math is hard. That's why we have Joelle!
00:20:46.260 Alright, let’s take a look! We have a... oh, you know, I like your voice better than mine! You do this.
00:20:55.160 I really join the club! Alright, 1, 2, and 3 in an array. We map over it, and then just send it some symbols; yeah, just a couple of them, like a keyboard mash.
00:21:07.510 So what’s the array? Is it 1, 2, and 3? No method error? Syntax error? 6?
00:21:22.040 Which math is hard, but 6 is if you added them all up, yeah, yeah. Cool! Now you do have your power-ups; you can pair and use Stack Overflow.
00:21:36.270 Yeah, um, so before I use one of those two, because I definitely am going to, we have this array—we called map!—and then we're sending it Plus at.
00:21:46.480 Uh, as a symbol, and then we’re printing. Okay, now you got it! I would think B or C because I don’t understand this, so my brain is having a syntax error.
00:22:02.640 I wouldn’t know; I would never do that to you, probably! But yeah, I think this is probably... it probably works somehow.
00:22:18.160 But I have no idea what, uh, so I would like to pair through all.
00:22:31.020 Alright, who thinks they can help Thomas out? Of course you do, Brandon! Brandon, what do you think?
00:22:45.230 You just ruined so much of my show! I know it would have been better with negatives, but that was in the last show, so now we’re doing...