From: Sam Roberts Date: 2006-10-21T02:33:24+09:00 Subject: Re: String not enumerable, what about IO? On Fri, Oct 20, 2006 at 01:24:29PM +0900, Michael Selig wrote: > 1) There are several methods in Array which I think could (should?) also be > in Enumerable (eg: uniq, +, | etc). > > 2) Presumably, as an alternative to most of the Enumerable methods, you > could use "to_a", and then use Array methods. I guess the Enumerable methods > would be more efficient than doing this, so is that the reason for using > them, or is it brevity or something else? Probably not more efficient, you have to build an Array internally to sort, for example. > 3) It has been mentioned before that Enumerable is a collection, which is > unordered (ie: you can't index them). However characters or lines in a > string or file *are* ordered. When you iterate over them you expect them to > come back in order. I think a[2] == a[2] should always be true, but there are things that are Enumerable that truly are unordered, and if Enumerable just did an #each_with_index pulled out the matching index, the above wouldn't be true. > 4) Other posts proposed having String#lines & String#bytes return an Array, > so the same efficiency comment as (2) applies. They could also return an > Enumerable object, which maybe more efficient but this relies on "each" > returning them in order (which it probably would!). Probably for Array, String, and IO, but Enumerable is used more than that. > Array methods available (delete, insert etc). May be a new module, rather > than polluting Enumerable? Maybe Arrayable? But remember that every method in Enumerable is implemented in terms of the underlying #each. How would you define a #[]= method in Enumerable? What method would it require the class to have? How would you even implement #[]= for a file, IO, or Socket? Sam