# Semantics of @x v. $x

**URL:** <https://www.ruby-forum.com/t/semantics-of-x-v-x/179584>\
**Category:** Ruby\
**Created:** [November 29, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/semantics-of-x-v-x/179584 "2009-11-29T15:54:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/semantics-of-x-v-x/179584/1 "2009-11-29T15:54:26Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 4:13pm UTC](https://www.ruby-forum.com/t/semantics-of-x-v-x/179584/2 "2009-11-29T16:13:44Z")

</div>

On Sunday 29 November 2009, Ralph S. wrote:

> |  
> |  
> |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

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/semantics-of-x-v-x/179584/3 "2009-11-29T22:02:33Z")

</div>

SC\> On Sunday 29 November 2009, Ralph S. wrote:

> > |  
> > |  
> > |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?  
> > |

SC\> @xyzzy = 5 defines an instance variable for the global object. That  
instance  
SC\> variable is accessible only from the top level. $xyzzy = 5, instead,  
defines a  
SC\> global variable, which can be accessed from everywhere.

SC\> Here’s an example:

SC\> @x = 1  
SC\> $y = 2

SC\> class C  
SC\>  
SC\> def test  
SC\> p defined?(@x)  
SC\> p defined?($y)  
SC\> p @x  
SC\> p $y  
SC\> end  
SC\>  
SC\> end

SC\> C.new.test

SC\> The output is:

SC\> nil  
SC\> “global-variable”  
SC\> nil  
SC\> 2

SC\> This shows that, while $y can be accessed even from instances of the  
C class,  
SC\> @x is accessible only from top level, that is only from the main  
object.

SC\> By the way, I suspect you forgot to paste a last line of output in  
your  
SC\> example. defined?($xyzzy) should have given you “global-variable”.

SC\> I hope this helps

SC\> Stefano

I thank you greatly for you explanation and your time.

Is there a document that you know of that describes these scoping  
rules for beginners?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [November 29, 2009, 10:16pm UTC](https://www.ruby-forum.com/t/semantics-of-x-v-x/179584/4 "2009-11-29T22:16:01Z")

</div>

On Sunday 29 November 2009, Ralph S. wrote:

> |\>\> |irb(main):004:0\> defined? $xyzzy  
> | everywhere.  
> |SC\> p defined?($y)  
> |SC\> nil  
> | “global-variable”.  
> |  
> |SC\> I hope this helps  
> |  
> |SC\> Stefano  
> |  
> |I thank you greatly for you explanation and your time.  
> |  
> |Is there a document that you know of that describes these scoping  
> |rules for beginners?  
> |

Well, these are among the basics of ruby, so any introduction to ruby  
should  
explain them. You can find the free version of Programming Ruby at  
[Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby) (it’s a bit old, but is  
mostly  
still valid). Then, there are a number of other documentation for  
beginners  
mentioned here [Ruby-Doc.org: Documenting the Ruby Language](http://www.ruby-doc.org/docs/). I can’t say which ones can  
be  
more useful, though.

Stefano
