From: Daniel Tse Date: 2005-09-28T15:12:13+09:00 Subject: Re: class variables v.nainar wrote: >Hello! > > What is wrong with the following code? Why is @@id incremented before >and not after the assignment? . ( Sorry if this sounds silly and I have >overlooked something obvious ) > > >#!/usr/local/bin/ruby >class Foo > @@id=1 > def initialize(s) > @s=s > @id=setId() > end > > You are assigning to the return value of setId, which is the incremented @@id: in Ruby, the implicit return value of a function is that of the last evaluated expression. > def setId() > @id=@@id > @@id+=1 > end >end > >foo1=Foo.new("one") >foo2=Foo.new("two") >foo3=Foo.new("three") >p foo1,foo2,foo3 >Output -> ># # why is @id not 1 ># # and 2 ># # and 3 ? > > > > > > > >