From: Jacob Fugal Date: 2006-02-24T01:38:19+09:00 Subject: Re: What's mean? global variables On 2/23/06, BlueFox 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 Fugal