From: Robert Klemme Date: 2004-08-11T16:11:14+09:00 Subject: Re: string.each_char "Martin DeMello" schrieb im Newsbeitrag news:72aSc.70461$gE.59700@pd7tw3no... > Alexander Kellett wrote: > > On Wed, Aug 11, 2004 at 03:21:15AM +0900, Martin DeMello wrote: > > > I really think String#each_char ought to be added to the core - it's one > > > of the most basic String operations, and in practice a lot more useful a > > > default case than each_byte. Sure, I can simulate it in a lot of > > > different ways, but I really do think it ought to be there by default. > > > > what are you attempting to archieve with it? > > "string".each_byte { |byte| puts byte.chr if byte == ?s } > > Iterating over a string letter by letter, without having to do > > string.split(//).each {} That iterates over an array of Strings(!). If you want to iterate over characters, you can use the algorithm Gavin showed. string.each_byte do |char| # either work with char which is a Fixnum or # convert it to a string (which is costly): str = char.chr end > I often find myself using a string as a compact data structure, > essentially a C style array of chars, and need to iterate over the > letters as an analogue to array#each{}. It feels clumsy to add an extra > layer of conversion. You then should treat charaters directly as Fixnum. This is much more efficient that converting each chaar to a one char string with #chr. Kind regards robert