Struct :symbol vs 'string' difference


(Yukihiro Matsumoto) #1

Hi,

···

In message “Struct :symbol vs ‘string’ difference” on 02/06/06, Tim Sutherland timsuth@ihug.co.nz writes:

ruby 1.6.6 (2001-12-26) [i586-linux]

Bar = Struct.new(‘Bar’, :a, :b, :c)
Bar.new(1, 2, 3) -> #<Struct::Bar a=1, b=2, c=3>

Foo = Struct.new(:Foo, :a, :b, :c)
Foo.new(1, 2, 3) -> #

I’m wondering why there is this difference, which I found surprising.

If the first argument to the “new” is a string, it will be the name of
the Struct class (as in Struct::Bar). It’s by the historical reason.
Ruby did not allow unnamed class before.

						matz.