From: Jim Weirich Date: 2002-07-08T21:24:26+09:00 Subject: Re: is there a better string.each? >>>>> "George" == George Ogata writes: George> We have methods corresponding to the iterator/collector methods to set the George> one that #each points to: #useBytes, #useChars (or #useCharacters), George> #useWords, #useLines, #usePars (or #useParagraphs). [...] George> s = "abc\ndef\nghi" George> s.useChars George> s.collect {|char| frob char} # iterates over chars George> s.useLines George> s.collect {|line| frob line} # now it iterates over lines The only problem I have with this suggestions is that useChars/useLines/etc are state changing methods that affect the behavior of "each" later in the program. Using "useChars" at one point may break a portion of code that assumes a line by line version of each. How about something like this. s = "abc\ndef\nghi" s.by_chars.collect { |char| frob char } s.by_lines.collect { |line| frob line } The methods by_chars/by_lines return an appropriate adapter object that does the indicated type of iteration over the target string. This means that there is no state information in a string that needs to change. The original s.each can continue to work as currently defined to avoid breakage. -- -- Jim Weirich jweirich@one.net http://w3.one.net/~jweirich --------------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)