Why can't I get an array range, like [3, 4], on a Struct?

S

Sam Roberts

I just noticed I can't do:

x = Struct.new(...
puts x[3, 4]

I'm doing this, instead, which works fine:

x = Struct.new(...
puts x.to_a[3, 4]

I guess it is because [] is not part of Enumerable, but why isn't []
defined by Enumerable? Is it because random access is so inefficient
when implemented with an iterator, for large collections, anyhow?

Maybe I answered my own question. :)

I vaguely remember some discussion of some modules that would allow a
class that supported to_str mix in support for all the other String
methods, and something similar for Array/to_a, as well.

I'd like to mix a little bit more of Array into Struct!

Cheers,
Sam
 
D

David A. Black

Hi --

I just noticed I can't do:

x = Struct.new(...
puts x[3, 4]

I'm doing this, instead, which works fine:

x = Struct.new(...
puts x.to_a[3, 4]

I guess it is because [] is not part of Enumerable, but why isn't []
defined by Enumerable? Is it because random access is so inefficient
when implemented with an iterator, for large collections, anyhow?

Maybe I answered my own question. :)

I vaguely remember some discussion of some modules that would allow a
class that supported to_str mix in support for all the other String
methods, and something similar for Array/to_a, as well.

I'd like to mix a little bit more of Array into Struct!

You can do that (just write a module and extend your Structs), though
in general I think Structs are more hash-like than array-like. If
there's a #[] method, I wouldn't expect it to be restricted to
integers. (Is the order of #to_a for Structs even guaranteed?)

(Tangential question:

How does one refer to the methods one defines on a Struct, or for that
matter its other instance methods? The usual ClassName#method_name
thing doesn't work :)


David
 
S

Sam Roberts

Quoteing (e-mail address removed), on Mon, Nov 15, 2004 at 09:00:18AM +0900:
I'd like to mix a little bit more of Array into Struct!

You can do that (just write a module and extend your Structs), though
in general I think Structs are more hash-like than array-like. If
there's a #[] method, I wouldn't expect it to be restricted to
integers. (Is the order of #to_a for Structs even guaranteed?)

Aha, so I expected it be more like an Array, and you expected it to be
more like a Hash! But, it is not quite either. :)

You can index by symbols (hash-like), or by integer (array-like).
Pickaxe has examples of using integer indexes that rely on
the ordering, for both #[] and #to_a, so I'm assuming it's intended to
be guaranteed. From ri1.8:

Returns the values for this instance as an array.

Customer = Struct.new:)name, :address, :zip) joe = Customer.new("Joe
Smith", "123 Maple, Anytown NC", 12345)

joe.to_a[1] #=> "123 Maple, Anytown NC"

(Tangential question:

How does one refer to the methods one defines on a Struct, or for that
matter its other instance methods? The usual ClassName#method_name
thing doesn't work :)

The thing Struct.new returns is enough of a class, isn't it?

irb(main):001:0> s = Struct.new('MyClass', :foo, :bar)
=> Struct::MyClass
irb(main):002:0> s.methods
=> ["send", "name", "display", "class_eval", "object_id",
...

Cheers,
Sam
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: Why can't I get an array range, like [3, 4], on a Struct?"

|Aha, so I expected it be more like an Array, and you expected it to be
|more like a Hash! But, it is not quite either. :)

Struct is a Struct is a Struct. You may want to check arrayfields.

http://raa.ruby-lang.org/project/arrayfields/

matz.
 
D

David A. Black

Hi --

Quoteing (e-mail address removed), on Mon, Nov 15, 2004 at 09:00:18AM +0900:
I'd like to mix a little bit more of Array into Struct!

You can do that (just write a module and extend your Structs), though
in general I think Structs are more hash-like than array-like. If
there's a #[] method, I wouldn't expect it to be restricted to
integers. (Is the order of #to_a for Structs even guaranteed?)

Aha, so I expected it be more like an Array, and you expected it to be
more like a Hash! But, it is not quite either. :)

I agree; ultimately I expect it to act like a Struct :)
(Tangential question:

How does one refer to the methods one defines on a Struct, or for that
matter its other instance methods? The usual ClassName#method_name
thing doesn't work :)

The thing Struct.new returns is enough of a class, isn't it?

irb(main):001:0> s = Struct.new('MyClass', :foo, :bar)
=> Struct::MyClass
irb(main):002:0> s.methods
=> ["send", "name", "display", "class_eval", "object_id",

What I meant was referring to a method in writing, the way we say
Array#push or String#split.


David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top