42

I have the following Ruby code:

local_var = "Hello"

def hello
  puts local_var
end

hello

I get the following error:

local_variables.rb:4:in 'hello': undefined local variable or method 'local_var' 
for main:Object (NameError) from local_variables.rb:7:in '<main>'

I always thought that local variables are not accessible from outside of the block, function, closure, etc.

But now I defined local variable in the file and try to get an access from the function INSIDE the same file.

What's wrong with my understanding?

3
  • 1
    You said "I always thought that local variables are not accessible from outside of the […] function". So why are you surprised that your local variable is only looking inside the function on the puts line?
    – Gareth
    Mar 12, 2012 at 17:15
  • 4
    because other programming languages differ Mar 12, 2012 at 19:36
  • 1
    try @local_var = "Hello" Jan 18, 2016 at 22:56

3 Answers 3

58

In Ruby local variables only accessible in the scope that they are defined. Whenever you enter/leave a Class, a Module or a Method definiton your scope changes in Ruby.

For instance :

v1 = 1

class MyClass # SCOPE GATE: entering class
  v2 = 2
  local_variables # => ["v2"]

  def my_method # SCOPE GATE: entering def
    v3 = 3
    local_variables  # => ["v3"]
  end # SCOPE GATE: leaving def

  local_variables # => ["v2"]
end # SCOPE GATE: leaving class

These entering and leaving points are called Scope Gates. Since you enter through Scope Gate via method definition you cannot access your local_var inside hello method.


You can use Scope Flattening concept the pass your variable through these gates.

For instance instead of using def for defining your method you can use Module#define_method.

local_var = "Hello"

define_method :hello do
  puts local_var
end

In the same way you can define your classes via Class#New so that your scope does not change when you pass through class definition.

local_var = 'test'

MyClass = Class.new do
  puts local_var #valid
end

instead of

class MyClass
  puts local_var #invalid
end

In the same way you should use Module#New if you want to pass your local variables through Module gates.

Example is taken from Metaprogramming Ruby

0
0

Ensure that the function takes arguments and add the object as one of the arguments when you call it

local_var = "Hello"

def hello(some_local_var)
  puts local_var
end

hello(local_var)

-7

local_var is a local variable. Local variables are local to the scope they are defined in. (That's why they are called "local variables", after all!) So, obviously, since local_var is defined in the script scope, you cannot access it in the method scope.

3
  • 24
    If I am in a car and I have change in my pocket, then there is change in the car. Plenty of other languages forward local variables to inner scopes, so there is no reason to treat Ruby's behavior as self evident. Apr 1, 2014 at 19:44
  • @AdamTolley you're totally right but I'm guessing Ruby might be Jorg's first language and the scoping might seem obvious to him! Mar 3, 2017 at 10:56
  • @AlexanderSuraphel: Actually, it was about my fifteenth language, although by the time I wrote that answer, I was probably up another dozen or so. I don't really keep track. Mar 3, 2017 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.