Class variables

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
  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 ->
# <Foo:0xb7d6b4f0 @id=2, @s="one"> # why is @id not 1
# <Foo:0xb7d6b4c8 @id=3, @s="two"> # and 2
# <Foo:0xb7d6b4a0 @id=4, @s="three"> # and 3 ?

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 ->
# <Foo:0xb7d6b4f0 @id=2, @s="one"> # why is @id not 1
# <Foo:0xb7d6b4c8 @id=3, @s="two"> # and 2
# <Foo:0xb7d6b4a0 @id=4, @s="three"> # and 3 ?

When somethings at the end of a function, it gets returned implicitly. So,
when you call @id - setId() you're setting @id to the current id (1), then
incrementing, and then then function returns the incremented value,
overwriting your 1 with a 2. Throw a return @s at the end and you'll get the
behaviour you're looking for.

···

On Wednesday 28 September 2005 00:03, 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
  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 ->
# <Foo:0xb7d6b4f0 @id=2, @s="one"> # why is @id not 1
# <Foo:0xb7d6b4c8 @id=3, @s="two"> # and 2
# <Foo:0xb7d6b4a0 @id=4, @s="three"> # and 3 ?

> 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 )

···

On Wed, Sep 28, 2005 at 03:52:54PM +0900, Kevin Brown wrote:

On Wednesday 28 September 2005 00:03, v.nainar wrote:

-------

> # <Foo:0xb7d6b4f0 @id=2, @s="one"> # why is @id not 1
> # <Foo:0xb7d6b4c8 @id=3, @s="two"> # and 2
> # <Foo:0xb7d6b4a0 @id=4, @s="three"> # and 3 ?

When somethings at the end of a function, it gets returned implicitly. So,
when you call @id - setId() you're setting @id to the current id (1), then
incrementing, and then then function returns the incremented value,
overwriting your 1 with a 2. Throw a return @s at the end and you'll get the
behaviour you're looking for.

Well it **was** silly of me .Thanks for the quick replies