From: Trans Date: 2005-08-08T19:11:09+09:00 Subject: Re: String#to_ary and Test::Unit Martin DeMello wrote: > Yukihiro Matsumoto wrote: > > > > I don't think providing to_ary for objects which are not really > > arrays. In this case, puts recurse for objects with to_ary, then each > > array returned from to_ary contains objects with to_ary (strings), > > then infinite recursion. > > I'd go further, and say that String should not implement Enumerable - as > it is, any code that wants to recursively traverse collections has to > include a check for String so that it doesn't get into the infinite > "String is a collection of Strings" recursion. each_byte, each_word and > each_line (without any default 'each') should provide most of the > functionality people need anyway - I can't remember ever needing to call > map or inject on a String unless I'd already #split or #scanned it into > an Array first. Yes and no. No b/c I don't see any reason for String not to have a _default_ representation as an array. The problem is that right now that "default" is dependent on a global setting $/ via its String's def of #each and its use of the #to_a method from Enumerable. Using a global smells. Globally changing the way #each behaves, as well as the methods that depend on it, is awefully fragile. It means you can't depend their behavior. I know that matz endlessly insists that string is not an array of chars, despite that other languages do represent them as such, that's fine wih me. String is not an Array. And so I understand about not having a String#to_ary. I was just trying to find a way around the above problem. So also Yes, I agree with you in that its hardly useful for String to include Enumerable --the way things are. But if String#to_a were defined as split(//), so that there was a _consistant_ result, then it would be quite useful. Unfortuately, one can't just redefine String#to_a in this way b/c, like I said, the whole thing is so fragile, and one might cause other code to break that depends on the globablly setable Enumerable version. T.