Demonstrate Ruby Procs / Lambdas vs Methods for Variable Scope

There are some key differences between procs / lambdas and traditional methods in Ruby, however the differences can many times be subtle. In this exercise we’ll examine how variable scope is processed differently between the different options.

Summary

Build a program that illustrates the differences in variable scope in Ruby between Procs / Lambdas, and traditional methods.

Exercise File

Code File

Exercise Description

Build a traditional method and a lambda or Proc and illustrate how the two processes have different access levels for local variables.

Real World Usage

This exercise showcases one of the subtle differences between Procs / lambdas and plain Ruby methods. At a high level it may seem like the three options can be used interchangeably, however in this guide we’ll walk through how they approach variable access much differently.

Solution

Can be found on the solutions branch on github.

Jordan Hudgens

view all post
Leave a comment
hiro • 4 months ago

Hi, Jordan. Very helpful tutorial. Clear things out really. By the way, I was very curious about the way editor(vim?) just commented each return value in an instant with the file execution(or something else?). What tool are you using here? Vim plugin?

reply
Jordan Hudgens • 4 months ago

It's the seeing_is_believing gem integration with Vim, I love it!

reply

Please be polite. We appreciate that.

By Daniele Zedda • 18 February

← PREV POST

By Daniele Zedda • 18 February

NEXT POST → 34
Share on