Ruby

Summarized using AI

Play with local vars

Tatsuhiro Ujihisa • April 18, 2019 • Fukuoka, Japan

The video titled 'Play with local vars' features a presentation by Tatsuhiro Ujihisa at RubyKaigi 2019, focusing exclusively on Ruby's local variables. The talk, lasting around 40 minutes, explores various aspects of local variables, including their behavior, pitfalls, and the dynamic nature of Ruby. Ujihisa begins with an engaging introduction, questioning the audience about their familiarity with local variables. Key points discussed include:

  • Types of Variables: The presenter distinguishes local variables from other variable types in Ruby, such as global, instance, and class variables.
  • Local Variables vs. Methods: Ujihisa emphasizes the intentional design of Ruby that blurs the line between local variables and methods, making them indistinguishable for users.
  • Common Pitfalls: He warns about using 'eval' for dynamic code execution, illustrating a scenario where improperly declared local variables can lead to a NameError, as seen in the example using 'eval'.
  • Using eval and Binding: The presentation covers the pitfalls of eval alongside the benefits of the Ruby binding method, which allows one to capture and manipulate local variable contexts effectively.
  • Flexibility of Local Variables: Ujihisa shares that local variables can act as method parameters and are highly flexible within Ruby programming. He discusses how understanding these nuances contributes to effective Ruby development.

Throughout the talk, Ujihisa utilizes an interactive coding demonstration in Vim to illustrate his points, providing a real-time coding experience that enhances understanding. He concludes by reiterating the power and flexibility inherent in Ruby's handling of local variables and expresses eagerness for questions and deeper discussions. In summary, the talk serves as an insightful exploration into the dynamics of Ruby's local variables and their importance in programming practices.

Play with local vars
Tatsuhiro Ujihisa • April 18, 2019 • Fukuoka, Japan

This 40min talk is only about Ruby's local variables. I'm not going to talk about anything else.

I'll demonstrate, or more precisely, play with Ruby local variables, including a discussion about the following common pitfall:

eval 'a = 1'
eval 'p a' # NameError!

Ruby has multiple different types of variables: global vars, instance vars, class vars, constants, local vars, and pseudo vars such as self. Identifiers without sigils such as @ are considered either local vars, methods, method arguments, block parameters, pseudo variables, or other reserved words. Particularly important aspect I believe is that Ruby intentionally tries to make local vars and methods indistinguishable for human. This talk focuses only on local vars among other variables or methods.

Keywords: parse.y, binding, yarv iseq, continuation, flip-flop operator, regular expression, and gdb
Oh by the way, the whole presentation is likely going to be done inside my Vim.

RubyKaigi 2019

00:00:00.179 My talk is scheduled for six minutes, so while you're waiting, you can try running my code snippet without executing it.
00:00:06.120 Are you ready to try? Great! Let's go.
00:00:44.180 My concern today is that I have a sore throat, and when I practiced, it took about 17 minutes.
00:00:51.350 I need to deliver the talk at a slower pace, but I'll do my best.
00:01:11.890 So, I have a question: how many of you here have never used a local variable before?
00:01:19.250 Raise your hand if you have never used a local variable.
00:01:25.520 Okay, it seems that everyone has used local variables.
00:01:31.190 Now, another question: do you fully understand local variables?
00:01:38.990 If you completely understand local variables, that’s great.
00:02:11.830 Here's a simple demonstration: Ruby allows you to write this kind of code.
00:02:21.560 This is just five lines of Ruby code. Technically, two lines should be hidden for better complexity.
00:02:36.410 What do you think the answer is? Just shout it out if you know.
00:02:43.820 Does anybody want to take a guess?
00:02:49.340 You're correct! Even with a warning, Ruby handles it.
00:03:00.920 This code captures a continuation and returns a string.
00:03:17.590 In this case, it returns a string with multiple values.
00:03:24.290 Let's play around with local variables in theory; this was my talk.
00:03:31.430 The abstract for my presentation, published on the RubyKaigi website, states that this presentation will occur mostly in Vim.
00:03:42.010 That helps me work quickly, allowing me to demonstrate code snippets effectively.
00:03:53.710 I'm currently using this empty buffer; watch as I type Ruby code and trigger it.
00:04:03.540 I assign this to be a variable, and then I run commands.
00:04:14.170 The process runs smoothly.
00:04:20.910 Hello everyone, my name is Tatsuhiro Ujihisa.
00:04:31.090 You can call me Woody.
00:04:38.020 I'm here to share insights about Ruby.
00:04:44.590 In the past, I have given talks, including a tiny talk in 2008 and 2009.
00:04:53.710 Additionally, I spoke about various topics including Ruby and code structure.
00:05:05.140 About two years ago, I discussed local variables in particular.
00:05:14.380 This year, I am excited to present my first talk on Ruby's local variables.
00:05:21.820 I am nervous but ready to share my experiences.
00:05:30.300 Currently, I work at a company in Creper, creating Ruby on Rails applications.
00:05:43.560 I've been involved in developing educational services using Ruby.
00:05:50.300 The product I work on is mainly utilized in Indonesia, the Philippines, and Mexico.
00:06:00.180 In Japan, we refer to it as Study Support.
00:06:08.430 We are also hiring, so if any of you know anyone, please send them my way.
00:06:15.760 I live part-time in Vancouver and Tokyo.
00:06:21.670 One favorite feature of Ruby is its dynamic nature.
00:06:28.570 There are many ways to declare and utilize local variables.
00:06:37.840 My talk will focus on different aspects of Ruby and local variables.
00:06:45.520 One of the simple ways is to assign values directly to variables.
00:06:52.210 For example, if we assign 1, 2, and 3 to X, it will be straightforward.
00:06:58.740 So, if you type X, it will reference the assigned values.
00:07:06.260 We can also use local variables as method parameters.
00:07:13.450 When calling a method with parameters, those parameters become local variables.
00:07:19.200 In Ruby, there are several special local variable declarations.
00:07:27.960 One common feature is using eval; everybody knows eval, right?
00:07:34.700 With eval, you can execute Ruby code dynamically.
00:07:42.890 However, keep in mind that using eval could lead to context issues.
00:07:50.840 If you don't properly declare variables before calling them, it can raise a NameError.
00:08:00.200 Thus, wrapping lines of code using eval has its potential pitfalls.
00:08:07.800 The Ruby binding method allows you to capture the local variable context.
00:08:15.560 Once captured, you can manipulate the binding context effectively.
00:08:21.400 This is handy, as it provides access to local variables.
00:08:28.900 In summary, this demonstrates the flexibility and potential complexity of Ruby's local variables.
00:08:39.940 I found that understanding these nuances helps in effective Ruby programming.
00:08:53.200 The dynamic nature of Ruby and its handling of local variables is a powerful feature.
00:09:05.780 I look forward to your questions and discussions about these concepts.
00:09:18.270 Thank you for your attention!
Explore all talks recorded at RubyKaigi 2019
+73