From: "David A. Black" Date: 2004-11-15T09:00:18+09:00 Subject: Re: Why can't I get an array range, like [3, 4], on a Struct? Hi -- On Mon, 15 Nov 2004, Sam Roberts wrote: > 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 -- David A. Black dblack@wobblini.net