String.each_char

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.

martin

what are you attempting to archieve with it?
"string".each_byte { |byte| puts byte.chr if byte == ?s }
=> s
when i first started i always thought the same
but i love the ?<letter> construct these days.

there is no basic char type. only bytes. and single letter strings.

mvg,
Alex

···

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.

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.

martin

···

Alexander Kellett <ruby-lists@lypanov.net> 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 }

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

···

On Wednesday, August 11, 2004, 6:16:14 AM, Martin wrote:

Alexander Kellett <ruby-lists@lypanov.net> 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.

"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:72aSc.70461$gE.59700@pd7tw3no...

> > 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

···

Alexander Kellett <ruby-lists@lypanov.net> wrote:
> On Wed, Aug 11, 2004 at 03:21:15AM +0900, Martin DeMello wrote:

Hi,

At Wed, 11 Aug 2004 05:16:14 +0900,
Martin DeMello wrote in [ruby-talk:108820]:

Iterating over a string letter by letter, without having to do

How do you define "char" or "letter"?

string.split(//).each {}

string.scan(/./m) {}

···

--
Nobu Nakada

Hm - good point. We'd need to go down the C path and separate 'char'
from 'string', so that the former could be Fixnum-type singletons. Yet
another place where C thinking doesn't map to ruby best-practice :slight_smile:

martin

···

Gavin Sinclair <gsinclair@soyabean.com.au> wrote:

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.

Hi,

At Wed, 11 Aug 2004 05:16:14 +0900,
Martin DeMello wrote in [ruby-talk:108820]:
> Iterating over a string letter by letter, without having to do

How do you define "char" or "letter"?

Something matching /./ :slight_smile:

> string.split(//).each {}

string.scan(/./m) {}

martin

···

nobu.nokada@softhome.net wrote:

"Martin DeMello" <martindemello@yahoo.com> schrieb im Newsbeitrag
news:pdjSc.72120$J06.71567@pd7tw2no...

>
> 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.

Hm - good point. We'd need to go down the C path and separate 'char'
from 'string', so that the former could be Fixnum-type singletons. Yet
another place where C thinking doesn't map to ruby best-practice :slight_smile:

Well, char and string *are* in fact separate. A char is simply a Fixnum.
Although I'd prefer a unique type for chars that can be better deal with
different encodings. IMHO the encoding issue is not fully satisfactory
solved in Ruby yet. To improve this is one of my major requirements for
Ruby 2. My 0.02 EUR...

Kind regards

    robert

···

Gavin Sinclair <gsinclair@soyabean.com.au> wrote: