Struct :symbol vs 'string' difference 
Author Message
 Struct :symbol vs 'string' difference

Hi,

In message "Struct :symbol vs 'string' difference"

|
|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) -> #<Foo Foo=1, a=2, b=3, c=nil>
|
|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.



Mon, 22 Nov 2004 23:42:10 GMT  
 Struct :symbol vs 'string' difference

Quote:
> 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) -> #<Foo Foo=1, a=2, b=3, c=nil>
> I'm wondering why there is this difference, which I found surprising.

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

--

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



Mon, 22 Nov 2004 23:42:10 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Oops: difference in operation of string.join and ''.join

2. Problems with LONG's and STRUCT's

3. Have you heard of 'struct'

4. tk4.1b3 installation problem 'struct timezone'

5. STRING 'make'/'remake'

6. kill '-SIGHUP', pgid -vs- kill 'SIGHUP', 0

7. Getting Ascii string from hex 'string'

8. Symbol '_eif_dynamic_routine'

9. Proably easy: 'symbol undefined' errors

10. 'command-key' (cloverleaf) symbol in mactk

11. Help: 'ld: Undefined symbol ___builtin_saveregs'

12. the difference between f : '' and f@]

 

 
Powered by phpBB® Forum Software