# What's mean? global variables

**URL:** <https://www.ruby-forum.com/t/whats-mean-global-variables/72352>\
**Category:** Ruby\
**Created:** [February 23, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/whats-mean-global-variables/72352 "2006-02-23T16:05:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BlueFox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a587f6/32.png) [@BlueFox](https://www.ruby-forum.com/u/BlueFox)\
**Post date:** [February 23, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/whats-mean-global-variables/72352/1 "2006-02-23T16:05:00Z")

</div>

in test.rb  
class Test  
def greeting  
“hello world!”  
end  
end

def test  
$test ||= Test.new  
end

and somewhere

puts test.greeting

what is mean of(outside of the class define)

def test  
$test ||= Test.new  
end

and where can find any document?

thanks.

---

<div class="post-metadata">

**Author:** ![BlueFox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a587f6/32.png) [@BlueFox](https://www.ruby-forum.com/u/BlueFox)\
**Post date:** [February 23, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/whats-mean-global-variables/72352/2 "2006-02-23T17:39:25Z")

</div>

On 2/23/06, BlueFox [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> what is mean of(outside of the class define)
> 
> def test  
> $test ||= Test.new  
> end

The keyword def, as you already know, begins a method definition. When  
the method definition occurs outside the scope of any class, the  
current ‘self’ is the ‘main’ object. Defining a method on ‘main’  
through some black magic, actually creates the method as an instance  
method of Object. As such, the method is available to all instances of  
Object – that is, everything. This way, the method can be called  
without a receiver in any context and generally have the same behavior  
(modulo instance variables, but if you’re using instance variables in  
top-level methods/procedures, you’re asking for trouble). This makes  
the method, in effect, a procedure.

$test is a global variable. Thats important in this case for scope –  
$test continues to exist and retain its value between invocations of  
test. The ||= idiom is one commonly found in ruby. It’s similar to +=  
in that it is simply short hand for “$test = $test || Test.new”. $test  
starts with a value of nil, and the first time test is run (unless you  
manually set $test beforehand), $test is initialized to a new instance  
of Test, since “(nil || anything) == anything”. On subsequent runs,  
$test retains its value. From this behavior, ||= can be thought of as  
a “default initializer” operator.

Does that answer your questions?

Jacob F.

---

<div class="post-metadata">

**Author:** ![BlueFox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a587f6/32.png) [@BlueFox](https://www.ruby-forum.com/u/BlueFox)\
**Post date:** [February 24, 2006, 4:41am UTC](https://www.ruby-forum.com/t/whats-mean-global-variables/72352/3 "2006-02-24T04:41:00Z")

</div>

Jacob F. wrote:

> $test is a global variable. Thats important in this case for scope –  
> $test continues to exist and retain its value between invocations of  
> test. The ||= idiom is one commonly found in ruby. It’s similar to +=  
> in that it is simply short hand for “$test = $test || Test.new”. $test  
> starts with a value of nil, and the first time test is run (unless you  
> manually set $test beforehand), $test is initialized to a new instance  
> of Test, since “(nil || anything) == anything”. On subsequent runs,  
> $test retains its value. From this behavior, ||= can be thought of as  
> a “default initializer” operator.

Jacob,

So is this a shortcut for creating a singleton in Ruby? Albeit a  
slightly different one I would guess, using a global variable to store a  
single instance rather than using a class method to instantiate a new or  
return the existing instance.

Thanks…  
-dave

---

<div class="post-metadata">

**Author:** ![BlueFox](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/a587f6/32.png) [@BlueFox](https://www.ruby-forum.com/u/BlueFox)\
**Post date:** [February 24, 2006, 7:49am UTC](https://www.ruby-forum.com/t/whats-mean-global-variables/72352/4 "2006-02-24T07:49:13Z")

</div>

> 

Oh! the test function just an another way to access the global variable  
$test?

yesterday i forgot that we can define a fuction outside classes(unlike  
java) and function’s return value is the last statement’s return value?

Thanks very much.

leo.
