From: Joshua Collins Date: 2009-03-31T08:33:17+09:00 Subject: Re: When to use instance variables @ --000e0cd56aa473873204665e8281 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Ah right. I totally forgot about instance auto set to nil ;) I see where you were coming from now. On Mon, Mar 30, 2009 at 7:27 PM, I=F1aki Baz Castillo wrote= : > El Martes 31 Marzo 2009, Joshua Collins escribi=F3: > > All variables in Ruby (Local, Global, Instance (object), or Class) do n= ot > > 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 =3D 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=F1aki Baz Castillo > > --000e0cd56aa473873204665e8281--