jimcgh
jimcgh

Reputation: 5957

Variable scope in Ruby

Consider the following two snippets of ruby code.

puts "One"
if false
  d = 1
end
puts "Two"
puts d
puts "Three"

This prints the following

One
Two

Three

Now, consider the following

[].each do |i|
  flag = false
end
puts "Two"
puts flag
puts "Three"

This gives the following

Two
'<main>': undefined local variable or method 'flag' for main:Object (NameError)

Why is it that in the first case a blank is printed and the 2nd case an error is thrown ?

Thanks

Upvotes: 5

Views: 940

Answers (2)

Paul Richter
Paul Richter

Reputation: 11072

The difference is that the if block isn't actually a separate scope like it is in other languages such as Java. Variables declared within the if block have the same scope as the surrounding environment. Now, in your case, that if block won't actually be executed, so you'd normally expect d to be undefined (resulting in the same error you got in the second example). But ruby is a little "smrt" in that the interpreter will set up a variable with that label the moment it sees it, regardless whether it is actually executed, because it essentially doesn't know just yet whether that branch will indeed execute. This is explained in "The Ruby Programming Language" by David Flanagan and Yukihiro Matsumoto (can't copy paste the text, adding screenshot instead): enter image description here

In the case of the .each loop, that do...end you've written in is actually a block, and it does have its own local scope. In other words, variables declared within a block are local to that block only.

However, blocks "inherit" the scope of the environment in which they're declared, so what you can do is declare flag outside of the .each iteration block, and then the block will be able to access it and set its value. Note that in the example you've given, that won't happen because you're attempting to iterate an empty array, but at the very least you won't receive an error any more.

Some additional reading:

Upvotes: 6

Maxim Pontyushenko
Maxim Pontyushenko

Reputation: 3043

In Ruby when you do an assignment to a variable (in your case it's d) anywhere in False-branch of If-statement it declares this variable unless method d= is defined. Basically b = bla-bla-bla in False-branch makes this: b = nil.

When you use block on an empty array nothing happens. And if an array is not empty variable still be local for current iteration of a block unless it was defined outside block scope, for example:

[1,2,3,4].each do |i|
  a=i
end
puts a

NameError: undefined local variable or method `a' for main:Object

a=1
[1,2,3,4].each do |i|
  a=i
end
puts a

4

Also you have an option to use a as a local one inside a block if it has been previously defined:

a=1
[1,2,3,4].each do |i; a|
  a=i
end
puts a

1

Upvotes: 2

Related Questions