Hi --

On Tue, 9 Jul 2002, Sean Russell wrote:

> Hal E. Fulton wrote:
>
> > Better perhaps to try it in Ruby awhile
> > fbefore making an RCR:
> >
> > class String
> >   def chars
> >     self.split("")
> >   end
> >   def each_char
> >     self.each_byte {|b| yield b.chr }
> >   end
> > end
>
>
> As I've said before, this is a nice solution, Hal.  However, I would expect
> that native support for this would be faster, and when one is itterating
> over characters, speed can quickly become an issue.  I was hoping for
> something faster than split(''), which is also not very memory efficient,
> creating an arbitrary Array object in the process.

I think part of the original #chars idea was that it would return an
array of the characters, as well as iterating over them.  The version
I posted a few posts back was:

  class String
    def chars
      split("").each {|c| yield c if block_given?}
    end
  end

(which really doesn't do much other than relocate the #each from the
caller to the body of the method, but that's what people seemed to
want :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav