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

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. :slight_smile:

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

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. :slight_smile:

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 :slight_smile:

David

···

On Mon, 15 Nov 2004, Sam Roberts wrote:

--
David A. Black
dblack@wobblini.net

Quoteing dblack@wobblini.net, 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. :slight_smile:

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 :slight_smile:

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

Hi,

···

In message "Re: Why can't I get an array range, like [3, 4], on a Struct?" on Mon, 15 Nov 2004 11:08:59 +0900, Sam Roberts <sroberts@uniserve.com> writes:

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. :slight_smile:

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

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

              matz.

Hi --

Quoteing dblack@wobblini.net, 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. :slight_smile:

I agree; ultimately I expect it to act like a Struct :slight_smile:

> (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 :slight_smile:

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

···

On Mon, 15 Nov 2004, Sam Roberts wrote:

--
David A. Black
dblack@wobblini.net