From: Gavin Sinclair Date: 2004-08-11T08:51:27+09:00 Subject: Re: string.each_char On Wednesday, August 11, 2004, 6:16:14 AM, Martin wrote: > 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 {} > 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. Presumably you use this "compact" structure because it's one object rather than many? But then, each time you iterate it, you're generating #{string.length} temporary objects. Perhaps it would be more "compact" in the long run if you used an array of strings or symbols? Or a Set, depending on your access requirements. # Or perhaps my presumption is wrong... Cheers, Gavin