# What is the $ in varaiable

**URL:** <https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348>\
**Category:** Ruby\
**Created:** [December 27, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348 "2007-12-27T21:20:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 27, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/1 "2007-12-27T21:20:48Z")

</div>

what is the meaning of the $ in the variable  
$tws = Twslinkrb::TWSLinkWrap.new(1)

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 27, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/2 "2007-12-27T21:23:44Z")

</div>

Junkone wrote:

> what is the meaning of the $ in the variable  
> $tws = Twslinkrb::TWSLinkWrap.new(1)

It marks the variable as global.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 27, 2007, 9:50pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/3 "2007-12-27T21:50:46Z")

</div>

On Dec 27, 3:22 pm, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Junkone wrote:
> 
> > what is the meaning of the $ in the variable  
> > $tws = Twslinkrb::TWSLinkWrap.new(1)
> 
> It marks the variable as global.
> 
> –  
> NP: Nile - The Infinity Of Stone  
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

how do global instance variables work. i am looking for some examples  
espcially how the scope of the varaibles work.  
thanks

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 27, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/4 "2007-12-27T22:00:46Z")

</div>

On Dec 27, 3:53 pm, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> NP: Ulver - Ulveratternes Kamp  
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

ok. if i have $something, is it available to all the objects withtin  
the program. for ex  
i have opened $something  
a.method invoks b.method.  
does $something be available inside b.method without passing it.  
can u point out some examples.  
thanks in advance.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 27, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/5 "2007-12-27T21:55:27Z")

</div>

Junkone wrote:

> how do global instance variables work.

There’s no such thing.  
If a var starts with $, it’s global, if it starts with @ it’s an  
instance var.  
It can’t be both.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 27, 2007, 11:58pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/6 "2007-12-27T23:58:50Z")

</div>

> if i have $something, is it available to all the objects withtin the program.

It is really totally global. You can access any global var from any  
method. 🙂

In many cases though, you will find that in “typical” ruby code there is  
not really the need to use many global variables (but instead local, and  
@instance vars)

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 27, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/7 "2007-12-27T22:05:27Z")

</div>

Junkone wrote:

> ok. if i have $something, is it available to all the objects withtin  
> the program.

Yes, that’s what global means.

> i have opened $something  
> a.method invoks b.method.  
> does $something be available inside b.method without passing it.

Yes.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [December 29, 2007, 11:05am UTC](https://www.ruby-forum.com/t/what-is-the-in-varaiable/124348/8 "2007-12-29T11:05:34Z")

</div>

On 27.12.2007 23:58, Marc H. wrote:

> > if i have $something, is it available to all the objects withtin the program.
> 
> It is really totally global. You can access any global var from any  
> method. 🙂

There are however some variables that are gloabal but thread local (all  
globals that are affected by regular expression matching).

> In many cases though, you will find that in “typical” ruby code there is  
> not really the need to use many global variables (but instead local, and  
> @instance vars)

And in fact, globals are often considered bad practice - not just in  
Ruby but more generally. 🙂

Kind regards

robert
