Hey Robert!
Let me add to that that usually you want to associate a variable with=20
the class instance or with instances. Class variables (the ones with=20
@@) have some strange properties that sometimes lead to surprising=20
behavior. I generally recommend to not use them. Instance variables i= n=20
classes are much simpler to handle and much clearer and cleaner IMHO.
Just to make things more clear,
irb(main):001:0> class Test
irb(main):002:1> @@v1 =3D 0 # class variable?
irb(main):003:1> @v2 =3D 1 # ???
irb(main):004:1> def initialize
irb(main):005:2> @v3 =3D 2 # instance variable?
irb(main):006:2> end
irb(main):007:1> def one
irb(main):008:2> @@v1
irb(main):009:2> end
irb(main):010:1> def two
irb(main):011:2> @v2
irb(main):012:2> end
irb(main):013:1> def three
irb(main):014:2> @v3
irb(main):015:2> end
irb(main):016:1> end
=3D> nil
irb(main):017:0> t =3D Test.new
=3D> #<Test:0xb7dc6574 @v3=3D2>
irb(main):018:0> t.one
=3D> 0
irb(main):019:0> t.two
=3D> nil
irb(main):020:0> t.three
=3D> 2
What is the correct names of v1, v2 and v3? v1 is a class variable, but=20
what about v2 and v3? We can say that v3 belongs to the class instance=20
(we saw that after creating the class v3 is on the output=20
<Test:0xb7dc6574 @v3=3D2>)on t, but v2 belongs to the class Test?
The answer is the difference to create the variables just after the=20
class statement and before the initialize method OR create it in the=20
initialize method, right?
And v2 is not reached by the two method, but if I change the class and=20
insert
irb(main):026:0> class Test
irb(main):027:1> def self.four
irb(main):028:2> @v2
irb(main):029:2> end
irb(main):030:1> end
and use
irb(main):032:0> t.class.four
=3D> 1
I can reach it. So where v1 and v2 exactly are named and belongs to? I=20
can see that each t haves it's own v3, but still didn't get the way the=20
or ones are. How v1 and v2 are located on different places.
Kind of
+-------+ +-----+
| Test +-----+ t |
+-------+ +-----+
| @@v1? | | @v3 |
| @v2? | +-----+
+-------+
Maybe v1 and v2 are on the same place (the Test instance - v1 needs to=20
be shared by all Test's, right?) but just can be reached on different=20
ways (v2 using four)?
On the pickaxe we have an example
class SongList
MaxTime =3D 5*60 # 5 minutes
def SongList.isTooLong(aSong)
return aSong.duration > MaxTime
end
end
On this case, MaxTime resides on the same place that v2 or not? Because=20
if I change SongList and add
class SongList
def isTooLong(aSong)
return aSong.duration > MaxTime
end
end
and use
s =3D SongList.new
s.isTooLong(<song here>)
I have a valid result, but something like
irb(main):057:0> class SongList2
irb(main):058:1> @maxtime =3D 5*60
irb(main):059:1> def SongList2.isTooLong(t)
irb(main):060:2> return t > @maxtime
irb(main):061:2> end
irb(main):062:1> def isTooLong(t)
irb(main):063:2> return t > @maxtime
irb(main):064:2> end
irb(main):065:1> end
=3D> nil
irb(main):066:0> s2 =3D SongList2.new
=3D> #<SongList2:0xb7d733d8>
irb(main):067:0> s2.isTooLong(301)
ArgumentError: comparison of Fixnum with nil failed
from (irb):63:in `>'
from (irb):63:in `isTooLong'
from (irb):67
from (null):0
irb(main):068:0> SongList2.isTooLong(301)
=3D> true
irb(main):069:0> SongList2.isTooLong(30)
=3D> false
gives me that error, so there is also a scope difference on a constant=20
like MaxTime and a variable like @maxtime, defined on the class body?
Thanks!