Hi,

In message "Re: Ruby 1.9/1.8 compatibility: String#lines"
    on Sun, 8 Apr 2007 01:11:23 +0900, murphy <murphy / rubychan.de> writes:
|
|> |It seems the most important change in 1.9, in terms of compatibility, is
|> |that String isn't Enumerable anymore. It breaks a lot of scripts on my
|> |system.
|> 
|> I put "lines" before calling "each".
|Maybe I didn't explain my problem well enough, sorry. If I do this, my
|script can run with Ruby 1.9, sure.
|
|But Ruby 1.8 says:
|
|  undefined method `lines' for "Foo\nbar":String (NoMethodError)
|
|so it won't run under both versions.
|
|I feel that many people (me, for a start) will want to make their
|libraries available for both versions in the next 2 or 3 years, if not
|longer.
|
|So I'm searching for a bullet-proof way to make String enumeration
|transparent from 1.8/1.9.
|
|The most simple way that comes to my mind is to add at least String#to_a
|to Ruby 1.9, with the Ruby 1.8 behavior. This would still force the
|people to adjust their libs, but at least they'll have an easy option
|not to break backwards compatibility instead.

I'd suggest adding lines method to return an array of lines in the
string, if it's not defined.

class String
  if defined? "".lines
    alias lines to_a
  end
end

If 1.8 maintainer agrees, I'd add the method to 1.8.

|I dare to say that otherwise, something like the "dirty fix" from my
|previous post is likely to show up in Rails 2.0, or whenever they decide
|to go 1.9 ;)
|
|> |I couldn't find an easy way to make scripts running under both 1.8 and 1.9:
|> |
|> |- String#each_line requires a block in 1.8, and writing each_line{}
|> |  isn't really beautiful.
|> 
|> If you want to iterate on lines in a string, each_line is the best
|> name to describe the behavior, I think.  YMMV.
|yes, it is, but the problem is that each_line doesn't provide the
|functionality of String#lines. it requires a block, and it doesn't
|return an enumerator. so using each_line doesn't do the trick either.
|
|by the way: I love the new Enumerators!

Thank you.  But you can't expect backward compatibility when you use
the new enumerators.  It will not be available during 1.8.

							matz.