From: dblack@... Date: 2006-10-20T10:51:30+09:00 Subject: Re: String not enumerable, what about IO? Hi -- On Fri, 20 Oct 2006, Michael Selig wrote: > I am fairly new to ruby, and I have just started listening to this mailing > list, so please don't flame me too much if I get things wrong! > > ----- Original Message ----- > From: "Mauricio Fernandez" >> >> What about: >> * defining String#to_enum, which would accept an argument (:lines, :chars > or >> :bytes) defaulting to :lines. Note that right now (in HEAD) > "foo".to_enum >> ends up using Kernel#to_enum, raising a NameError (undefined #each for > class >> String). >> * making str.lines (chars, bytes) equivalent to >> str.to_enum(:lines).to_a (respectively :chars, :bytes) > > Though I have never used it, doesn't Ruby already have something (admitedly > rather long-winded) to do this called Enumerable::Enumerator? > > I would also like to add my 2 cents worth to this discussion. > > I have found Enumerables rather confusing, as there are many variants of > "each" in a number of different classes. That's sort of the point :-) All of the other enumerable methods -- select, reject, inject, etc. -- are built on top of each. If you define an each method for your class and mix in Enumerable, you get all the enumerable methods for free. > What I would prefer (and this would probably be a major change to the > implementation) is to be able to treat ALL enumerable classes as Arrays. In > other words, I'd like to suggest that Array and Enumerable effectively be > the same thing, and that "Array" would be the superclass instead of > "Enumerable". Enumerable isn't a superclass; it's a module. Don't forget you can mix it in to your own classes, so it's not just a matter of the behavior of Ruby core classes that are Enumerable. Also, in some respects Array is the common currency of Enumerables, in the sense that a lot of Enumerable methods return arrays. But that doesn't mean that Array itself is more Enumerable, so to speak, than other Enumerable classes. > Of course this would only be on the surface - underneath the implementation > of each would probably have to be different for efficiency. > > Thius would add possibly useless methods like Range#[], but why not? Because they're useless. I'd like to see things refactored a bit in Enumerable and the related classes (e.g., push each_with_index down into the individual classes), but I would not like to see useless methods. > Most enumerable classes are ordered, so the Array#[] method would > work fine. Hashes are an exception, and obviously Hash#[] would be > different. I'm not getting a clear picture of what you're proposing. How would I write an Enumerable class? It sounds like I'd have to subclass Array, which definitely isn't equivalent to what Enumerable offers. David -- David A. Black | dblack@wobblini.net Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org