Struct Question

Hello,

i have a question about Structs , in the Book ‘Ruby in a Nutshell’ is written
=>
S = Struct::new(:foo, :bar)
s = S::new(1,2)
s => #

but

i receive with my Code Ruby v1.6.8 =>

tem = Struct::new(:foo, :bar)
meinstruct = tem::new(0,8)
puts meinstruct

=> #<#<Class 0lx4013ac0c>>

is this a change in a Rubyversion ? Or did i read the book wrongly ?

mfg. Jonas Hoffmann

Hi, Jonas,

From: “Jonas Hoffmann” ruby@joelh.de
Sent: Thursday, December 26, 2002 6:58 PM

i have a question about Structs , in the Book ‘Ruby in a Nutshell’ is written
=>
S = Struct::new(:foo, :bar)
s = S::new(1,2)
s => #

but

i receive with my Code Ruby v1.6.8 =>

tem = Struct::new(:foo, :bar)
meinstruct = tem::new(0,8)
puts meinstruct

=> #<#<Class 0lx4013ac0c>>

is this a change in a Rubyversion ? Or did i read the book wrongly ?

No. Assignment to a constant is a key.
In your code, an object which ‘tem’ refers is still an anonymous class.

Regards,
// NaHi

Hello Jonas,

Thursday, December 26, 2002, 12:58:16 PM, you wrote:

i have a question about Structs , in the Book ‘Ruby in a Nutshell’ is written
=>>
S = Struct::new(:foo, :bar)
s = S::new(1,2)
s => #

but

i receive with my Code Ruby v1.6.8 =>

tem = Struct::new(:foo, :bar)
meinstruct = tem::new(0,8)
puts meinstruct

=>> #<#<Class 0lx4013ac0c>>

is this a change in a Rubyversion ? Or did i read the book wrongly ?

  1. use Capitalized name for class (Tem instead of tem)
  2. book shows result of “p s”, not “puts s”

Tem = Struct::new(:foo, :bar)
meinstruct = Tem::new(0,8)
p meinstruct

will work fine

···


Best regards,
Bulat mailto:bulatz@integ.ru

Ahh, i see thanks :slight_smile:

Jonas

···

Am Donnerstag, 26. Dezember 2002 11:15 schrieb Bulat Ziganshin:

is this a change in a Rubyversion ? Or did i read the book wrongly ?

  1. use Capitalized name for class (Tem instead of tem)
  2. book shows result of “p s”, not “puts s”

Tem = Struct::new(:foo, :bar)
meinstruct = Tem::new(0,8)
p meinstruct

will work fine