On 10/16/06, James Edward Gray II <james / grayproductions.net> wrote:
> On Oct 16, 2006, at 3:06 PM, Rick DeNatale wrote:
>
> > I'm curious as to why it's a good thing to make String nonenumerable
> > and remove String#each instead of just aliasing lines to each?  Are
> > there drawbacks which make it worthwhile breaking existing code?
>
> Well, now that we're stepping into the M17N world it no longer makes
> sense to work with Strings until we specify a unit for the content.
> Each what?  Each character, line, or byte?

Well the new methods bytes, lines, and soon chars give you enumerators
which can iterate over a specified unit.

In Ruby 1.8 the default  unit for String#each is lines.  I don't see
how that's incompatible with M17N.

> I really think this is a good move.  Every book that introduces
> String#each says something like, "You probably don't expect this, but
> Strings iterate over lines..."  Now it will always be crystal clear:

Yes I was surprised when I discovered that too. Stepping back though,
it's consistent with viewing String as duck-typable with IO rather
than as an array of characters.

Personally I disagree that this is a good move,  why break existing
code if it's not necessary?  There may be reasons why leaving String
an Enumerable breaks things, but I can't think of any.  I'd vote for
putting Enumerable back in string, and making lines an alias for each.

Somewhat on the subject, I'm not sure I understand the rational for
having the pairs of methods like each_byte and bytes not being aliases
of each other:

$ irb1.9
irb(main):001:0> "abc".each_byte
=> #<Enumerable::Enumerator:0xb7d7a340>
irb(main):002:0> "abc".bytes
=> #<Enumerable::Enumerator:0xb7d778fc>
irb(main):003:0> "abc".each_byte {|a| nil}
=> "abc"
irb(main):004:0> "abc".bytes {|a| nil}
=> #<Enumerable::Enumerator:0xb7d690b8>

Notice that String#bytes and String#lines ignore a block if it's
given.  On the other hand each_byte and each_line give an enumerator
if no block is given, which is consistent with the way such methods
work in 1.9 for classes which are still enumerable.  I don't see a
reason for just making bytes, each_bytes; chars, each_char, and lines,
each_lines pairs of aliased methods.


-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/