# String#each

**URL:** <https://rubytalk.org/t/string-each/551>\
**Category:** ruby-talk\
**Created:** [7 July 2002 23:04 UTC](https://rubytalk.org/t/string-each/551 "2002-07-07T23:04:03Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [7 July 2002 23:04 UTC](https://rubytalk.org/t/string-each/551/1 "2002-07-07T23:04:03Z")

</div>

Well, let’s come full circle and get back  
to “each” (which started the discussion).

The rationale for the current behavior (and  
someone else could explain this better) is  
that the “elements” of which a string is  
composed are subject to debate. In some  
situations, a string could be considered a  
sequence of lines.

There are each\_byte and each\_line iterators,  
in fact. So what should ‘each’ do? One or  
the other. Matz picked iteration by line.  
It’s just the default.

If you specifically want bytes or lines,  
you can use the specific iterator.

Yes, it was a surprise to me, too. But I  
have gotten used to it and I accept it.

If Matz changes it, I will accept that  
and get used to that, too. He is good at  
what he does.

A related topic:

I don’t know much about m17n or i18n… but  
I was wondering if an each\_char iterator  
might be a good idea for the future? That  
way we could distinguish between bytes and  
characters.

I favor making software as language-flexible  
as possible, but I’d hate to see it slow  
things down or complicate the API too much.

But I’m wandering outside my realm of knowledge.

Hal Fulton

---

<div class="post-metadata">

**Author:** ![Tom\_Sawyer](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tom\_Sawyer](https://rubytalk.org/u/Tom_Sawyer)\
**Post date:** [8 July 2002 01:05 UTC](https://rubytalk.org/t/string-each/551/2 "2002-07-08T01:05:08Z")

</div>

do you think its just over the top to change how .each works ?  
or is that just too much code breakage?

also, how is support for strings going to change in the future with the  
adaptation of i17m (i18n?) support.

~transami

> **···**
>
> On Sun, 2002-07-07 at 17:04, Hal E. Fulton wrote:
> 
> > Well, let’s come full circle and get back  
> > to “each” (which started the discussion).
> > 
> > The rationale for the current behavior (and  
> > someone else could explain this better) is  
> > that the “elements” of which a string is  
> > composed are subject to debate. In some  
> > situations, a string could be considered a  
> > sequence of lines.
> > 
> > There are each\_byte and each\_line iterators,  
> > in fact. So what should ‘each’ do? One or  
> > the other. Matz picked iteration by line.  
> > It’s just the default.
> > 
> > If you specifically want bytes or lines,  
> > you can use the specific iterator.
> > 
> > Yes, it was a surprise to me, too. But I  
> > have gotten used to it and I accept it.
> > 
> > If Matz changes it, I will accept that  
> > and get used to that, too. He is good at  
> > what he does.
> > 
> > A related topic:
> > 
> > I don’t know much about m17n or i18n… but  
> > I was wondering if an each\_char iterator  
> > might be a good idea for the future? That  
> > way we could distinguish between bytes and  
> > characters.
> > 
> > I favor making software as language-flexible  
> > as possible, but I’d hate to see it slow  
> > things down or complicate the API too much.
> > 
> > But I’m wandering outside my realm of knowledge.
> > 
> > Hal Fulton
> 
> –  
> ~transami
> 
> “They that can give up essential liberty to obtain a little  
> temporary safety deserve neither liberty nor safety.”  
> – Benjamin Franklin

---

<div class="post-metadata">

**Author:** ![Mark\_Slagell](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Mark\_Slagell](https://rubytalk.org/u/Mark_Slagell)\
**Post date:** [8 July 2002 02:05 UTC](https://rubytalk.org/t/string-each/551/3 "2002-07-08T02:05:18Z")

</div>

> do you think its just over the top to change how .each works ?  
> or is that just too much code breakage?

Whenever there’s some defensible reason for why something works as it  
does, even if there are pretty good arguments that it would have been  
better if it had been done some other way, we need to lean heavily  
toward preserving the status quo for that behavior.

For my purposes anymore, basically any code breakage is too much. I sure  
don’t want to worry about things going wrong with scripts that are in  
production use whenever I upgrade the interpreter, and I’ve been relying  
heavily on Ruby at work for quite a while.

Mark

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [8 July 2002 03:51 UTC](https://rubytalk.org/t/string-each/551/4 "2002-07-08T03:51:45Z")

</div>

Hi,

> **···**
>
> In message “Re: String#each” on 02/07/08, Tom Sawyer [transami@transami.net](mailto:transami@transami.net) writes:
> 
> > also, how is support for strings going to change in the future with the  
> > adaptation of i17m (i18n?) support.
> 
> String index and length will be based on characters, not bytes. There  
> will be no other incompatible changes.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Massimiliano\_Mirra4](https://avatars.discourse-cdn.com/v4/letter/m/43a26b/32.png) [@Massimiliano\_Mirra4](https://rubytalk.org/u/Massimiliano_Mirra4)\
**Post date:** [8 July 2002 11:24 UTC](https://rubytalk.org/t/string-each/551/5 "2002-07-08T11:24:09Z")

</div>

Imagine this…

There is a 16compat.rb file in ruby1.8 lib directory. By requiring  
it, all language features that have been changed (not added) in 1.8  
assume 1.6 behaviour. You could put a require ‘compat16’ in your  
script, and you would be sure that, if it ran under 1.6, it will run  
for you. You could even set that system-wide to be sure (e.g. `ruby' is really a script containing `ruby -rcompat1.6’) and require  
‘compat18’ for new projects. Most important, by doing so on the new  
project, whatever change will happen in ruby2.0 you already know your  
script will still run because compat18.rb in ruby2.0 will take care of  
it…

Just a thought.

Massimiliano

> **···**
>
> On Mon, Jul 08, 2002 at 11:05:18AM +0900, Mark Slagell wrote:
> 
> > Whenever there’s some defensible reason for why something works as it  
> > does, even if there are pretty good arguments that it would have been  
> > better if it had been done some other way, we need to lean heavily  
> > toward preserving the status quo for that behavior.
> > 
> > For my purposes anymore, basically any code breakage is too much. I sure  
> > don’t want to worry about things going wrong with scripts that are in  
> > production use whenever I upgrade the interpreter, and I’ve been relying  
> > heavily on Ruby at work for quite a while.

---

<div class="post-metadata">

**Author:** ![SER1](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@SER1](https://rubytalk.org/u/SER1)\
**Post date:** [9 July 2002 03:45 UTC](https://rubytalk.org/t/string-each/551/6 "2002-07-09T03:45:30Z")

</div>

Yukihiro Matsumoto wrote:

> > also, how is support for strings going to change in the future with the  
> > adaptation of i17m (i18n?) support.
> 
> String index and length will be based on characters, not bytes. There  
> will be no other incompatible changes.

So, even with i17m support, there will be no easy way to itterate over  
characters? There will be no efficient way to break the string up into its  
atomic parts?

– SER

---

<div class="post-metadata">

**Author:** ![Tom\_Sawyer](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tom\_Sawyer](https://rubytalk.org/u/Tom_Sawyer)\
**Post date:** [8 July 2002 11:30 UTC](https://rubytalk.org/t/string-each/551/7 "2002-07-08T11:30:54Z")

</div>

Massimiliano, THAT’S GENIUS!!!

> **···**
>
> On Mon, 2002-07-08 at 05:24, Massimiliano Mirra wrote:
> 
> > On Mon, Jul 08, 2002 at 11:05:18AM +0900, Mark Slagell wrote:
> > 
> > > Whenever there’s some defensible reason for why something works as it  
> > > does, even if there are pretty good arguments that it would have been  
> > > better if it had been done some other way, we need to lean heavily  
> > > toward preserving the status quo for that behavior.
> > > 
> > > For my purposes anymore, basically any code breakage is too much. I sure  
> > > don’t want to worry about things going wrong with scripts that are in  
> > > production use whenever I upgrade the interpreter, and I’ve been relying  
> > > heavily on Ruby at work for quite a while.
> > 
> > Imagine this…
> > 
> > There is a 16compat.rb file in ruby1.8 lib directory. By requiring  
> > it, all language features that have been changed (not added) in 1.8  
> > assume 1.6 behaviour. You could put a require ‘compat16’ in your  
> > script, and you would be sure that, if it ran under 1.6, it will run  
> > for you. You could even set that system-wide to be sure (e.g. `ruby' is really a script containing `ruby -rcompat1.6’) and require  
> > ‘compat18’ for new projects. Most important, by doing so on the new  
> > project, whatever change will happen in ruby2.0 you already know your  
> > script will still run because compat18.rb in ruby2.0 will take care of  
> > it…
> > 
> > Just a thought.
> > 
> > Massimiliano
> 
> –  
> ~transami
> 
> “They that can give up essential liberty to obtain a little  
> temporary safety deserve neither liberty nor safety.”  
> – Benjamin Franklin

---

<div class="post-metadata">

**Author:** ![Tom\_Sawyer](https://avatars.discourse-cdn.com/v4/letter/t/a587f6/32.png) [@Tom\_Sawyer](https://rubytalk.org/u/Tom_Sawyer)\
**Post date:** [8 July 2002 19:03 UTC](https://rubytalk.org/t/string-each/551/8 "2002-07-08T19:03:10Z")

</div>

Has no one else read this post?

> **···**
>
> On Mon, 2002-07-08 at 05:24, Massimiliano Mirra wrote:
> 
> > Imagine this…
> > 
> > There is a 16compat.rb file in ruby1.8 lib directory. By requiring  
> > it, all language features that have been changed (not added) in 1.8  
> > assume 1.6 behaviour. You could put a require ‘compat16’ in your  
> > script, and you would be sure that, if it ran under 1.6, it will run  
> > for you. You could even set that system-wide to be sure (e.g. `ruby' is really a script containing `ruby -rcompat1.6’) and require  
> > ‘compat18’ for new projects. Most important, by doing so on the new  
> > project, whatever change will happen in ruby2.0 you already know your  
> > script will still run because compat18.rb in ruby2.0 will take care of  
> > it…
> > 
> > Just a thought.
> > 
> > Massimiliano
> 
> i’m waiting to here what others think. i am hopeful that this will be in  
> our ruby-futures.
> 
> perhaps this idea should be moved to a new thread?
> 
> ~transami

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [9 July 2002 04:13 UTC](https://rubytalk.org/t/string-each/551/9 "2002-07-09T04:13:30Z")

</div>

Hi,

> **···**
>
> In message “Re: String#each” on 02/07/09, Sean Russell [ser@germane-software.com](mailto:ser@germane-software.com) writes:
> 
> > So, even with i17m support, there will be no easy way to itterate over  
> > characters? There will be no efficient way to break the string up into its  
> > atomic parts?
> 
> Other than split(//)?  
> It will have each\_char method too.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Tobi\_Reif](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Tobi\_Reif](https://rubytalk.org/u/Tobi_Reif)\
**Post date:** [9 July 2002 08:14 UTC](https://rubytalk.org/t/string-each/551/10 "2002-07-09T08:14:17Z")

</div>

Yukihiro Matsumoto wrote:

> It will have each\_char method too.

cool 🙂

That will be great for iterating over the characters. What will be a  
good way to get an array of characters from multibyte strings? (probably  
something that already exists)

Tobi

> **···**
>
> –  
> [http://www.pinkjuice.com/](http://www.pinkjuice.com/)

---

<div class="post-metadata">

**Author:** ![SER1](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@SER1](https://rubytalk.org/u/SER1)\
**Post date:** [9 July 2002 13:46 UTC](https://rubytalk.org/t/string-each/551/11 "2002-07-09T13:46:27Z")

</div>

Yukihiro Matsumoto wrote:

> Other than split(//)?  
> It will have each\_char method too.

Bingo.

— SER

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [9 July 2002 08:32 UTC](https://rubytalk.org/t/string-each/551/12 "2002-07-09T08:32:47Z")

</div>

Hi,

> **···**
>
> In message “Re: String#each” on 02/07/09, Tobias Reif [tobiasreif@pinkjuice.com](mailto:tobiasreif@pinkjuice.com) writes:
> 
> > That will be great for iterating over the characters. What will be a  
> > good way to get an array of characters from multibyte strings? (probably  
> > something that already exists)
> 
> Not yet. Anyone?
> 
> chars, explode\_chars, … hmm
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Tobi\_Reif](https://avatars.discourse-cdn.com/v4/letter/t/dbc845/32.png) [@Tobi\_Reif](https://rubytalk.org/u/Tobi_Reif)\
**Post date:** [9 July 2002 08:38 UTC](https://rubytalk.org/t/string-each/551/13 "2002-07-09T08:38:56Z")

</div>

Yukihiro Matsumoto wrote:

> chars, explode\_chars, … hmm

Intuitively, I’d try

irb(main):001:0\> ‘sue’.to\_a  
[“sue”]

, but that does not return an array of characters…

chars might be a good name.  
Or chary 🙂 (char ary)

Tobi

> **···**
>
> –  
> [http://www.pinkjuice.com/](http://www.pinkjuice.com/)

---

<div class="post-metadata">

**Author:** ![SER1](https://avatars.discourse-cdn.com/v4/letter/s/3e96dc/32.png) [@SER1](https://rubytalk.org/u/SER1)\
**Post date:** [9 July 2002 13:46 UTC](https://rubytalk.org/t/string-each/551/14 "2002-07-09T13:46:25Z")

</div>

Yukihiro Matsumoto wrote:

> > That will be great for iterating over the characters. What will be a  
> > good way to get an array of characters from multibyte strings? (probably  
> > something that already exists)
> 
> Not yet. Anyone?
> 
> chars, explode\_chars, … hmm

Well, split() is fine with me in this case. I don’t think it is unintuitive  
from breaking a String up into an array; I just didn’t think it was very  
intuitive or efficient for itterating over characters. In this case,  
you’re making a new array anyway, so this would be fine for me.

If you, Matz, know that there’s a more efficient way of creating an Array of  
Characters (which, I assume, will be Strings) from a String, then yes. I  
would like to have a chars() method. However, if everything is basically  
going to boil down to an internal split(), can we just use split()?

— SER

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [9 July 2002 16:30 UTC](https://rubytalk.org/t/string-each/551/15 "2002-07-09T16:30:23Z")

</div>

Hi,

> **···**
>
> In message “Re: String#each” on 02/07/09, Sean Russell [ser@germane-software.com](mailto:ser@germane-software.com) writes:
> 
> > If you, Matz, know that there’s a more efficient way of creating an Array of  
> > Characters (which, I assume, will be Strings) from a String, then yes. I  
> > would like to have a chars() method. However, if everything is basically  
> > going to boil down to an internal split(), can we just use split()?
> 
> If you want to split a string into per character strings, you should  
> (and you will) use String#split. chars (and each\_char) work on  
> “characters”, which are integers in my current M17N implementation.
> 
> ```
> matz.
> 
> ```
