Skip to content
Douglas G. Allen edited this page Jan 15, 2015 · 2 revisions

Home

# http://www.rubyist.net/~slagell/ruby/localvars.html
# local
$foo
#=> nil
@foo
#=> nil
foo
NameError: undefined local variable or method 'foo' for main:Object

# http://www.rubyist.net/~slagell/ruby/globalvars.html
# global
$foo
#=> nil
$foo = 5
#=> 5
$foo
#=> 5

# http://www.rubyist.net/~slagell/ruby/instancevars.html
# instance
class InstTest
  def set_foo(n)
    @foo = n
  end

  def set_bar(n)
    @bar = n
  end
end
#=> :set_bar
i = InstTest.new
#=> #<InstTest:0x83678>
i.set_foo(2)
#=> 2
i
#=> #<InstTest:0x83678 @foo=2>
i.set_bar(4)
#=> 4
i
#=> #<InstTest:0x83678 @foo=2, @bar=4>

# http://www.rubyist.net/~slagell/ruby/constants.html
# class
fluid=30
#=> 30
fluid=31
#=> 31
Solid=32
#=> 32
Solid=33
#=> warning: already initialized constant Solid
Clone this wiki locally