On Fri, 13 Aug 2004, Warren Brown wrote: > With all of the recent discussions on String#each_char, I have been > thinking... > > There is a need for processing the characters of a string with > each(), but in a way shorter and more efficient than string.split(//) or > string.scan(/./m). There is also a need to have the definition of > "character" be "codepoint" under M17N. One solution to all of this > would be a new method of String, let's call it String#chars, that would > efficiently translate a string into an array of "characters". I am not > fluent in all of the issues involved with multi-byte characters, but a > parameter to String#chars could control the definition of "character", > and could default to generating an array of single-byte characters (like > string.split(//)). > > This would allow users who want to iterate over the individual > single-byte characters to write "string.chars.each", and those wishing > to split the string into codepoints could write > "string.chars(something).each" to select the correct definition of > "character". > > This solution would seem to provide the maximum flexibility, > allowing constructs like "string.chars.sort", while maintaining > compatibility with the current behavior of String#each. However, much > of the benefit would depend on the speed at which the translation to an > array could be done. > > Comments? something like this ~ > cat b.rb class String def chars width = 1 unpack "a#{ width }" * (size / width + (size % width == 0 ? 0 : 1)) end end s = 'foobar' p s.chars p s.chars(3) ~ > ruby b.rb ["f", "o", "o", "b", "a", "r"] ["foo", "bar"] ?? -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================