Struct :symbol vs 'string' difference


(Thomas Hurst) #1

If Struct#new’s first argument is a string, it’s used as the name of the
structure within Struct::, otherwise it’s just returned as an anonymous
class object.

It’s not surprising if you read the docs :wink:

···

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.


Thomas ‘Freaky’ Hurst - freaky@aagh.net - http://www.aagh.net/

She missed an invaluable opportunity to give him
a look that you could have poured on a waffle.