On Sunday 29 November 2009, Ralph Shnelvar wrote: > |Newbie here: > | > |Consider > |irb(main):001:0> @xyzzy = 5 > |=> 5 > |irb(main):002:0> defined? @xyzzy > |=> "instance-variable" > |irb(main):003:0> $xyzzy = 5 > |=> 5 > |irb(main):004:0> defined? $xyzzy > | > | > |What, semantically, is the difference between $x and @x when at top > |level? (Am I at top level?) > | > |Do $xyzzy and @xyzzy have global scope at statement 5 and beyond? > | @xyzzy = 5 defines an instance variable for the global object. That instance variable is accessible only from the top level. $xyzzy = 5, instead, defines a global variable, which can be accessed from everywhere. Here's an example: @x = 1 $y = 2 class C def test p defined?(@x) p defined?($y) p @x p $y end end C.new.test The output is: nil "global-variable" nil 2 This shows that, while $y can be accessed even from instances of the C class, @x is accessible only from top level, that is only from the main object. By the way, I suspect you forgot to paste a last line of output in your example. defined?($xyzzy) should have given you "global-variable". I hope this helps Stefano