MichaelHajuddah
MichaelHajuddah

Reputation: 557

Confused about instance variable scope, top-level vs. class

I've been playing around with Ruby mostly in the top level and I typically write code like:

@x = 'foo'
def show_var
  puts @x
end

show_var # => foo

I thought that instance variables were available to both the Class and the Object, based off how this example works.

Today I ran into this, and it looks like my understanding of instance variables is incorrect:

class Test
  @x = "foo" #you would need to define this inside 'initialize' for this to be available to show_var
  def show_var
    puts @x
  end 
end

Test.new.show_var # => nil

It looks like the second example is how instance variables work. If you define an instance variable inside the Class, then it only exists inside that scope, and does not exist for instance methods.

Then my question is... why does the first case output 'foo' when the variable @x shouldn't exist inside the scope of an instance method? Also, what is the proper way for defining variables in the top-level Class that you want to use for your top-level methods?

Upvotes: 4

Views: 917

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118271

Look below:

@x = 'foo'
def show_var
  puts @x,self
  p defined? @x
end

show_var
# >> foo
# >> main
# >> "instance-variable"

In the first case you defined @x in the main scope. And then when call the method show_var,from the main,thus you got the output of @x,which is not the case of the other.

class Test
  @x = "foo" 
  def show_var
    p @x,self 
    p defined? @x
  end 
end

Test.new.show_var
# >> nil
# >> #<Test:0x9b6fcd4>
# >> nil

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230336

The method in the first example is at the top level. Which means it belongs to a special top level object main. You can't really create more copies of main, so self inside and outside of that method is the same. Check this out:

self # => main
def show_var
  self # => main
end

class Test
  self # => Test
  def show_var
    self # => #<Test:0x007fdf9c892d70>
  end 
end

Upvotes: 5

Related Questions