From: "Iñaki Baz Castillo" Date: 2009-03-31T08:27:57+09:00 Subject: Re: When to use instance variables @ El Martes 31 Marzo 2009, Joshua Collins escribió: > All variables in Ruby (Local, Global, Instance (object), or Class) do not > need to be defined at the beginning of a method. I didn't say "at the beginning of a method", but "at the beginning of a class" (anyhow I was also wrong XD). > In Ruby, do you need to set that variable before calling it? Yes. Well, this is different for class variables and instance variables: -------------- irb> @non_existing_instance_variable nil irb> @@non_existing_class_variable NameError: uninitialized class variable @@non_existing_class_variable in Object from (irb):2 from /usr/bin/irb:12:in `
' -------------- What I mean is that class variables MUST be defined (not declared, of course) before *reading* them, while an instance variable returns nil if it's not set yet. > #No need to define the variable before it's value is set > @@number_of_squares = 1 Yes, please note that I said: ---------- This is not true when using class variables (@@). These need to be "declared" at the beginning of the class ---------- (note the "declared" between "" XD) Regards. -- Iñaki Baz Castillo