From: David Garamond Date: 2004-02-26T22:07:06+09:00 Subject: Re: Why don't $global and @instance variables need *initialization*? Robert Klemme wrote: >>Sorry if the answer is obvious, but I can't find a satisfactory or more >>verbose explanation about this. The only thing I found is from the >>Ruby's User Guide: >> >>"As with globals, instance variables have the nil value until they are >>initialized. Instance variables of ruby do not need declaration. This >>implies a flexible structure of objects. In fact, each instance variable >>is dynamically appended to an object when it is first referenced." >> >>Could someone explain the above? How is it more flexible? > > You don't need the declaration. That saves typing and also you can add > them at runtime as you like: Oops, my bad English strikes again. The question should have been: "why don't $global and @instance need *initialization* (i.e. they are already preset to nil)?" In other words, why does it behave like this? irb(main):001:0> a NameError: undefined local variable or method `a' for main:Object from (irb):1 irb(main):002:0> a=nil; a => nil irb(main):003:0> $a => nil irb(main):004:0> @a => nil ... and not like this? irb(main):001:0> a NameError: undefined local variable or method `a' for main:Object from (irb):1 irb(main):002:0> a=nil; a => nil irb(main):003:0> $a NameError: undefined global variable `$a' from (irb):3 irb(main):004:0> $a=nil; $a => nil irb(main):005:0> @a NameError: undefined instance variable `@a' from (irb):5 irb(main):006:0> @a=nil; @a => nil -- dave