Ruby Syntax

Summarized using AI

Who Wants to be a Ruby Engineer?

Drew Bragg • October 26, 2023 • Boulder, CO

The video "Who Wants to be a Ruby Engineer?" features Drew Bragg hosting a Ruby game show at the Rocky Mountain Ruby 2023 conference. The game involves contestants guessing the output of various Ruby code snippets, specifically those sourced from the more obscure aspects of the Ruby programming language.

Key Points Discussed in the Video:

  • Introduction of the Host and Game Background: Drew Bragg introduces himself, mentioning his role at Podia and his involvement in the Philadelphia Rubyists Meetup. He sets the stage for the game show format, explaining that the tasks will be challenging and that contestants will have options to help them answer the questions.
  • Game Structure: Contestants guess the output of Ruby code snippets with four multiple choice options. They can ask for audience help or use a Stack Overflow lifeline. Contestants who answer incorrectly are applauded for their effort and sit down.
  • Examples of Ruby Code Snippets: The game includes numerous snippets, two of which are:
    • An example with the syntax P = 1::A + (2), where contestants learn it outputs 3 due to Ruby's namespace resolution.
    • The pluck method on an array of hashes returns an array with specific values, illustrating how Ruby methods can behave in unexpected ways.
  • Mistakes and Learning: When contestants err, explanations follow that elucidate why certain outputs occur, emphasizing the importance of understanding Ruby's quirks and the objective of improving coding practices over writing confusing code. For instance, using shovel with an integer converts it to its ASCII representation, leading to insightful discussion about readability versus cleverness in coding.
  • Audience Interaction and Contestant Dynamics: Drew involves the audience for feedback and encourages lively participation, highlighting the fun aspect of learning together through competition.

Conclusions and Takeaways:

  • Encouragement of Continuous Learning: The game reinforces that even seasoned Ruby developers can learn from puzzling constructs, showcasing the community's movement towards better coding practices rooted in clarity and understanding.
  • Appreciation of Ruby's Flexibility: Drew expresses love for Ruby's expressive nature while simultaneously cautioning against the use of its more obscure aspects in production environments.
  • Community Building: By hosting the game show and including various participants, Drew fosters connections within the Ruby community. He invites viewers to share their unique Ruby syntaxes to learn collectively.

Overall, the session is a mix of humor, interactive learning, and a community spirit centered on enhancing Ruby coding proficiency.

Who Wants to be a Ruby Engineer?
Drew Bragg • October 26, 2023 • Boulder, CO

Rocky Mountain Ruby 2023 - Who Wants to be a Ruby Engineer? by Drew Bragg

Welcome to the Ruby game show where contestants tries to guess the output of a small bit of Ruby code. Sound easy? Here's the challenge: the snippets come from some of the weirdest parts of the Ruby language. The questions aren't easy but get enough right to win a fabulous prize.

Rocky Mountain Ruby 2023

00:00:14 Welcome to the last talk of day one of Rocky Mountain Ruby! Who is having fun so far? I don't know about you all, but I am very happy that I didn't have to fly across an ocean to see my Ruby friends. Thank you!
00:00:26 Just give me a second to catch up. Anyway, this presentation is titled, 'Who Wants to be a Ruby Engineer?' It is a Ruby game show because I'm not that talented to give an entire talk. I can pretty much do game shows and clap with one hand.
00:00:53 Hold on, technical difficulties—it's a tech conference! That's my face; it's really big. I'm so sorry! Anyway, my name is Drew Bragg. Contrary to popular belief, I am from Pennsylvania, right outside of Philadelphia—not from anywhere in Oklahoma.
00:01:11 I work at an awesome company called Podia as a senior engineer. Podia is fantastic. I get to work with incredible engineers, on an awesome product, and I love my job. I also host a monthly podcast called 'Code and the Coders Who Code.' If the name of this talk didn't give it away, I'm really bad at naming things.
00:01:37 I also help co-organize the Philly RB Meetup, which is the Philadelphia Rubyists Meetup. You don’t have to be from Philly to attend; we're fully virtual right now. We have people from all over the world. If you're interested in hanging out with some Rubyists, we meet every second Tuesday of the month.
00:01:56 You can also find me on the internet as Dr. Bragg on most platforms—Twitter, or X, or whatever the hell it’s called today, Mastodon, BlueSky, and GitHub. I forget the rest.
00:02:08 I was supposed to press the button and make the Podia logo come up because they sent me here, and they are just awesome—are you sensing a theme?
00:02:22 Before we get started, a few disclaimers: number one, as previously stated, I am terrible at naming things. When I had to come up with a name for the game show, I thought of other game shows where I could fit Ruby into their names. I thought of 'Who Wants to Be a Millionaire?' and realized that instead of a millionaire, I'll do Ruby engineer.
00:02:46 Just like with 'Who Wants to Be a Millionaire,' winning at this game does not make you a Ruby engineer. These questions are deliberately hard. I scoured the Ruby docs and the Ruby source code to find the weirdest bits of syntax to put into this silly little game show for you all.
00:03:07 So if you get none of them right (or none of them wrong), please let me know because you're amazing! But if you get none of them right, do not feel bad; that's intentional.
00:03:13 Also, almost all of the Ruby syntax you’re going to see today is valid. That means if you put it into IRB, it’ll run. I don’t recommend doing it for work, but if you want to play code golf with Ruby, these might be great! If you want to confuse your co-workers, that’s fine too.
00:03:32 If you want to have a little fun and maybe learn how Ruby works under the hood, these snippets are awesome. But I would think twice before putting them into production.
00:03:45 I love Ruby; it's an expressive language, allowing me to write it in a variety of ways and get away with some really weird stuff. But as professionals, we owe it to ourselves, our future selves, and our coworkers to write the most readable Ruby possible. AKA none of the weird stuff you're about to see!
00:03:58 A couple of rules I’ll get out of the way: we have some lovely contestants here in the front row who have graciously volunteered to let me abuse them for a little bit. They will have a few seconds—20 or so—to guess the output of a short snippet of Ruby.
00:04:19 I am not a monster; they will have multiple choice options: A, B, C, or D. So they have a 25% chance of getting it right just by guessing! I've also given them some power-ups. They can pair with someone from the audience to help them, or they can search Stack Overflow.
00:04:32 Spoiler alert: you all are Stack Overflow! If a contestant gets a question wrong, they will sit down, and we will applaud them because they were brave enough to come up on stage and learn something.
00:04:51 That should always be applauded! The last little bit of this: get enough questions right, and win a fabulous prize. I left all my prizes in Pennsylvania—I'm so sorry! I will mail them to you.
00:05:03 Doing great so far! Without any further ado, who is ready to play, 'Who Wants to be a Ruby Engineer?'
00:05:08 Can I get my first contestant? Come on down! Anyone can go in order, that's fine. Hey there!
00:05:20 Please introduce yourself. Tell us who you are, where you’re from, and how long you’ve been working with Ruby.
00:05:31 I'm Alise Schaefer. I live in Denver, but I'm originally from Pittsburgh, Pennsylvania, and I've been doing Ruby for 14 years. All right, are you ready to play?
00:05:43 Awesome! All right, this first one is a little bit of a warm-up so that we all know how this is going to go and the folks in the back can see whether they can actually read my syntax.
00:05:54 If we look up at the screen, we have a snippet: 'P = 1::A + (2)'. This is valid Ruby.
00:06:10 So when we run this, are we going to get back a three? Are we going to raise a no method error? Are we going to get 12 or a syntax error?
00:06:24 What do you think?
00:06:35 I think that joke about switching to PHP might not be a joke. I don't know if PHP is any better than this.
00:06:46 I feel like I want to say D, but that feels too obvious to be true. I’m going to go with D.
00:07:03 So it's not—it's a three! This is valid Ruby.
00:07:07 Now, a quick explanation for those of you who are scratching your heads: the double colon, as you may know, is a namespace resolution operator. We can traverse constants and namespaces with it, but we can also use it to call instance methods.
00:07:21 Since '+' is an instance method defined on Integer and '1' is an instance of Integer, you can do the math.
00:07:33 For the love of math, I am begging you: please just use one plus two!
00:07:45 Thank you so much for laughing so hard at that stupid joke!
00:07:54 All right, are you ready to play for real? Yes? Okay, here we go!
00:08:05 We have an array with a couple of hashes—named 'Matts' and 'Spike.' We’re going to call 'pluck' with the symbol 'name.' What are we going to get back?
00:08:20 Is it going to be an array with the strings 'Matts' and 'Spike'? A string just of the first one: 'Matts'? Are we going to get back nil, or are we going to raise a no method error?
00:08:27 Do you think you know what's going on? Umm, can I pull the audience?
00:08:40 Let's pull the audience! Absolutely! Audience: Stack Overflow! Who thinks this is going to be A? Okay... who thinks it’s B? Very small amount of people. Who thinks it’s C? And who thinks it’s D?
00:08:55 Okay, and who has seen this show before? All right, well, so what do you think?
00:09:06 Well, I feel like the D crowd is a much larger group, so I’m going to go with that—I’ll trust them.
00:09:17 That is correct! Thank you all; you're amazing!
00:09:24 For those of you who guessed A, that is part of Active Support. This is Rocky Mountain Ruby, not Rocky Mountain Rails. You should be in Amsterdam right now if you wanted Rails!
00:09:39 Very nice work! Are you ready for your next one? Doesn't matter, you're getting it anyway!
00:09:50 So, we have a variable 'str' that is an empty string. We're going to shovel the integer 97 into it. What is str going to be?
00:10:03 Are we going to get a type error, the string '97,' conversion? No problem; it's going to become a lowercase 'a' or the integer 97, no longer a string. What do you think?
00:10:16 You can’t search Stack Overflow anymore, but you can still pair.
00:10:27 I feel pretty confident in this, but that probably means I’m going to get it wrong. I'm going to go with C.
00:10:42 Yes! All right, be honest: show of hands. Who would have gotten that right?
00:10:56 So it turns out that if you shovel the operator on a string and you pass it an integer, it treats it like an ASCII code point. 97 is a lowercase 'a'. So you go lowercase!
00:11:06 I've seen this; don't do it. Why would you do this?
00:11:14 Next question! You're doing great! Awesome! We have an array with integers 1, 2, and 3 in it. We’re going to multiply it by 2 or use the asterisk by 2, however you would say that.
00:11:28 Are we going to get back an array that has 2, 4, and 6 in it? An array with 1, 2, 3, 1, 2, 3? Are we going to get an array of arrays, each with 1, 2, and 3 in it? Or is this a syntax error?
00:11:42 You can still pair, but no Stack Overflow.
00:11:54 Can I pair with Brooke? Yes!
00:12:03 Absolutely.
00:12:04 What do you think? Shout it out!
00:12:18 Unfortunately, it's B!
00:12:30 When you multiply an array by an integer, it acts like concatenating that many copies of that array. Thank you so much for playing!
00:12:39 I need a new contestant! Come on down! What's up? Same deal as last time: introduce yourself—where you're from, and how long you've been working with Ruby.
00:12:57 I'm John, I'm from Columbus, Ohio. You're going to see me tomorrow, and I've been doing Ruby for about seven years.
00:13:16 Okay, are you ready to play? I'm terrifically ready!
00:13:23 All right, let's do it! Same basic concept, but instead of a two, we have the string with a dash.
00:13:33 Are we going to get back an array of strings: '1-2-3'? Are we going to get back an array with d strings, '-1-2-dash-3'? Or are we going to get back an array that has now negated those numbers: -1, -2, -3? Or the string '1-2-3'?
00:13:57 You still have all of your power-ups; you can search Stack Overflow or you can pair.
00:14:10 I was really hoping there was going to be an error. I'm going to search Stack Overflow, alright.
00:14:19 Stack Overflow, you know the drill! Who thinks it's A?
00:14:34 One lone person in the back. Who thinks it's B? Even less people. Who thinks it’s C?
00:14:46 A few folks, and who thinks it’s D?
00:14:56 Oh, I mean, you’re going with A, obviously! D? It's got to be D—it’s got to be D!
00:15:05 Stack Overflow would never fail me. Never! Has Stack Overflow ever gotten anything wrong ever?
00:15:19 Okay, wait! I need to learn first because you've been doing Ruby for 10 years! Maybe you're right.
00:15:30 The answer is B. If you open up IRB right now, that’s what you'll get. But if for some reason you have Ruby 1.8 or older running, you'll get 146 because of the ASCII code point for the single character.
00:15:45 We already learned that a lowercase 'a' is 97; '1' is 49. You add those together. I know math is hard!
00:15:55 But it's 146! I've checked.
00:16:00 Stay up here, please! Nice work!
00:16:12 All right, we got another one for you. I'll make it a little easier because Regexes are super easy!
00:16:17 Foo is equal to string bar. We're going to do some regex nonsense using =~ on the string Foo. So when we call Print Foo, what are we going to get?
00:16:26 The string 'bar', the string 'Foo', the letter 'F', or the letter 'B'? This is valid Ruby!
00:16:35 Wait! I needed this to be some sort of true or false; this is not... no! You can’t pair! You can't use Stack Overflow! It’s all on you—no pressure!
00:16:48 Yeah... I was thinking that that would be some sort of comparison operator, but that seems to not be true.
00:17:00 You would be wrong! I thought that that question mark was some sort of capture operator, but there's no way it can be A...
00:17:12 It's F! I mean C! I mean C!
00:17:26 100% named Capt! Yes! This creates named captures in your regex, and using the =~ operator will create a local variable for you, overwriting any other local variable.
00:17:39 It’s horrible; don't do it! It sounds cool on paper: you're like, 'Code golf? Awesome!' No!
00:17:46 All right, so you're on a roll! You ready for another question?
00:17:57 Nope! Okay, well, you're getting one anyway. This bit is fun! I'm not even going to read this! I’m just going to let you soak it in a little bit.
00:18:08 What are we going to get: two, three, and four? Three, one, and five? A syntax error, which would be my guess? What is a range over true and false?
00:18:26 No! It's always true!
00:18:38 Luckily you're in Colorado.
00:18:44 Uh! I don’t know, D? That makes no sense!
00:18:52 It makes no sense? Or is it a syntax error?
00:19:04 100%! No, it's valid Ruby. It’s okay.
00:19:14 I actually found something like this in production code at my last job!
00:19:22 Do you know how long it takes to Google something like this when you don't know what the operator is called?
00:19:32 Listen! You belong up here! I didn’t know this was a flip-flop operator! It turns out this is a flip-flop operator!
00:19:44 The flip-flop is used in loops with conditionals when the first part of the flip-flop operator evaluates to true until the second part evaluates to true.
00:19:56 The conditional will evaluate to true and this was deprecated. They were going to take this out of Ruby, but it turns out it’s great for parsing libraries, so it’s kept.
00:20:08 And you no longer even get a deprecation warning about it! It’s a real thing!
00:20:28 Thank you very much for playing!
00:20:36 Round of applause! I learned something!
00:20:53 Thank you so much! Be careful on those stairs!
00:21:00 I need another contestant! Come on down!
00:21:12 All right! Same drill as last time, please introduce yourself. Tell us where you're from and how long you've been working with Ruby.
00:21:27 Hey, I’m Patrick. I’m from Louisville, Colorado. I think I've been using Ruby for like 15 years, but I’m so old I don’t remember.
00:21:40 So, we have the method num equal to five. We have a variable 'a' that is an array of 1, 2, and 3.
00:21:58 We're going to do a for loop of 'num' in 'a' and then we’re going to print whatever 'P' actually is. I think it’s print num.
00:22:07 Is the output going to be a name error, five, three, or nil? You do have your power-ups.
00:22:27 Stack Overflow has been pretty good.
00:22:43 I feel okay. I feel like the first line defines 'num' as a method that returns five.
00:22:54 That is exactly what that is! Great work! And the second one is an array of integers, yes!
00:23:05 Yes, exactly! So, this is where it gets hard. That’s a for loop. Oh man! I love this show!
00:23:25 What you’re not supposed to do is iterate like that in Ruby. You should always use 'each!'
00:23:37 So, I'm going to Stack Overflow!
00:23:50 All right! Stack Overflow, you know the drill! Who thinks it’s A?
00:23:59 One person! Who thinks it’s a name error?
00:24:05 There’s a lot of people!
00:24:07 F? It’s a lot of people. C? Slightly less people. D? Nil?
00:24:14 A couple of people, more than A though.
00:24:23 All right, so the Bs have it!
00:24:36 Okay, I trusted you all. No! Thank you.
00:24:55 Same thing as like with the Regex, right? I gotta stay! So this is actually a very good reason not to use the for loop.
00:25:05 Using .each will create a new scope when you pass it a block, but 'for' doesn’t do that!
00:25:16 And it will override local variables and methods!
00:25:34 Wait, what? I know this is chaos! Who wrote this language?
00:25:49 Matt! Great work! Thank you! Round of applause!
00:25:52 I need another contestant! Come on down!
00:26:03 All right! Please introduce yourself and tell us where you're from and how long you've been working with Ruby.
00:26:18 My name is Corey. I'm from Bailey, Colorado. I’ve been working with Ruby for 12 years.
00:26:32 Are you ready to play? I think so! All right! Let's find out!
00:26:53 Lord P! Bang! Open paren, close paren. That’s how I’m choosing to read that.
00:27:05 Are we going to get a name error, a syntax error, nil, or true?
00:27:14 I didn’t know you could negate nothing! Let's see if I have Stack Overflow!
00:27:29 Stack Overflow! You know the drill! Who thinks it’s A? One lone person. Who thinks it’s B? A syntax error!
00:27:49 I thought I was going to get more hands on that one. C: nil? There are a few shy hands. And D: true?
00:28:06 All right, let’s go with D!
00:28:21 I stared at this, Kevin Newton showed this to me, and I was like, 'What the...?' It turns out you can create a void context with just the empty parameters.
00:28:33 In Ruby, that is nil. If you negate nil, you actually get true, so that’s what just happened.
00:28:44 Great work! Can't blame Stack Overflow, but you can still pair!
00:29:02 All right, there's the brain! This is Ruby! This is not brain-n. I did not just curse! That’s actually the name of the programming language.
00:29:15 Do you need—I'm not sure if I can read this either!
00:29:23 Open parenthesis, close parenthesis. Question mark, question mark, question mark, colon, bang question mark!
00:29:39 Something like that. A: no method error? I’m assuming one of those is correct!
00:29:53 Let me think about this one! Right! We’re working with some cursed Ruby code maybe!
00:30:00 Okay, I got it! I figured it out! Took a while, but yes! A: no method error! I'm assuming!
00:30:15 Sure!
00:30:24 Everyone is doing great! A: no method error! B: The string question mark! C: False! Or D: nil!
00:30:36 You cannot use Stack Overflow, but you can still pair with Brooke!
00:30:51 What do you think?
00:31:00 So, you think it’s A? I would never do anything like that!
00:31:17 Go with A! That’s false!
00:31:47 This is a little bit of fun with whitespace! If we add the whitespace, it gets a little bit easier.
00:31:59 Does anybody see what’s going on here? We got a Turner!
00:32:12 If we evaluate those without using any special syntax, there you go!
00:32:27 Fair enough! It makes sense, right? Totally! I caught the Turner part, not the other garbage.
00:32:40 All right! Thank you very much for playing! Round of applause! But I need another contestant!
00:32:52 Come on down!
00:33:04 There we go! Gusto crew in the house! All right! Please introduce yourself, tell us where you're from, and how long you've been working with Ruby.
00:33:19 My name's Brian. I’ve been using Ruby for 18 years!
00:33:37 It's a long time—a very long time. I’m going to embarrass myself now!
00:33:53 What version of Ruby did you start with? 186!
00:34:03 So you would have—no, you were doing addition! I would have lied!
00:34:20 Let's break down the first question: proc is equal to proc curly brace, curly brace, hello, proc! Curly brace, P proc!
00:34:36 Close bracket! There’s a lot of proc!
00:34:50 Okay, all right. Let’s help a little bit! Syntax error, the string proc, the hash with the key-value pair hello and proc?
00:35:00 Or argument error?
00:35:05 I’m trying to think about it here.
00:35:20 So we got this hash, and you hear it! So let’s think it out.
00:35:30 We call the proc equal to the proc and that’s kind of messing with me!
00:35:40 Isn’t that kind of like the now that x equals x?
00:35:45 That was totally different than this.
00:36:05 All right, the square brackets: is that an A call? Is that invoking the proc? I have no idea!
00:36:20 Man, no, seriously! This is great! I wrote this question because I honestly have no clue!
00:36:35 It doesn’t seem like we really call the proc, so B doesn’t seem right.
00:36:54 ( we’re just accessing a hash! But if I were using the square bracket access on proc, is there an accessor on the proc?
00:37:05 Then that would be a no method error, which isn’t one of my choices!
00:37:20 So it’s awesome! I love when people think out loud like this! My superpower is being wrong in public!
00:37:33 But you’re doing great! No! Thank you!
00:37:50 But, hey! The audience! You all think I can’t keep track of it anymore!
00:38:02 All right! Stack overflow! Who thinks it’s A?
00:38:15 Couple people! Who thinks it’s B? A couple too!
00:38:27 Who thinks it might be C? Slightly more people!
00:38:39 And who thinks it’s D? Dang! That was helpful!
00:38:52 Thanks! Stack overflow! I'm pretty sure there was an even amount of hands for every s!
00:39:05 So, what do you think? Let’s go with C because you think the square brackets are actually invoking the proc!
00:39:17 But the first statement doesn't utilize the argument being passed to proc called hello.
00:39:29 It’s just returning the hash.
00:39:39 I like the way you think! You are correct!
00:39:47 All right! So for me, there’s a lot going on here because I’m not nearly as smart as that guy!
00:39:55 All right! So let’s get you another question!
00:40:07 A is equal to sleep 1.5. So what is A going to give us? One, nil, two, or the system will sleep for 1.5 seconds every time we call A?
00:40:19 No more Stack Overflow! You can pair!
00:40:30 This is the last question I have! I have no idea what sleep takes!
00:40:44 But okay, so you’re calling sleep is a method!
00:40:56 Yes, on Kernel! And you’re invoking it with 1.5, but you’re assigning so you’re going to get evaluated!
00:41:03 This the that you’re going to sleep, and then you're going to assign the return value to A!
00:41:16 So the system will sleep for 1.5 seconds! So I think D is right!
00:41:30 But the question is: what is the return value of sleep?
00:41:41 So I don’t know! I’m going to scan the audience!
00:41:55 There's a few people! I'm going to call up Patrick! Come on!
00:42:08 You think it’s B? You think there’s no return?
00:42:24 And that makes sense! It's like it's a side effect! So what could you possibly return from that side effect?
00:42:39 So I’ll go for B!
00:42:52 My favorite part about this question is you have a 50% chance of getting it right!
00:43:07 So, sleep does return the actual amount of time slept rounded!
00:43:23 If you do 1.5 or 2.5 or anything, you'll get either or depending on what else is going on in your system.
00:43:39 Bravo! Bravo! I don't know why... we know why!
00:43:54 Big round of applause for all of the contestants! Thank you so much!
00:44:04 No! We don't need any loud noises!
00:44:08 Thank you so much, guys! This was a lot of fun!
00:44:19 I had some help coming up with this from my good friends Ufuk, Kevin Murphy, Matias, Richard, and Kevin Newton.
00:44:26 All contributed some of this stuff, so don't just blame me! You can blame any of them or Matt!
00:44:42 This is me again: If you want to connect, I love Rubyists.
00:44:55 I love hanging out and finding out weird things, so if you have any weird syntax and want to send it my way, please do!
00:45:07 So we can stump someone else with it!
00:45:12 That's it!
Explore all talks recorded at Rocky Mountain Ruby 2023
+10