# String.each

**URL:** <https://www.ruby-forum.com/t/string-each/87503>\
**Category:** Ruby\
**Created:** [March 3, 2007, 4:35am UTC](https://www.ruby-forum.com/t/string-each/87503 "2007-03-03T04:35:43Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannick\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@Yannick\_G](https://www.ruby-forum.com/u/Yannick_G)\
**Post date:** [March 3, 2007, 4:35am UTC](https://www.ruby-forum.com/t/string-each/87503/1 "2007-03-03T04:35:43Z")

</div>

Hello all!

I’m fairly new to Ruby, and I’m trying to write a program that looks at  
each character of a string and then processes it using a block. I’ve  
been using:

String.each do  
#block  
end

but something isn’t working. I’m sure that there is a simple answer, but  
I’m not that experienced with the language. If someone could please help  
me out, I’d greatly appreciate it.

---

<div class="post-metadata">

**Author:** ![Yannick\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@Yannick\_G](https://www.ruby-forum.com/u/Yannick_G)\
**Post date:** [March 3, 2007, 4:42am UTC](https://www.ruby-forum.com/t/string-each/87503/2 "2007-03-03T04:42:09Z")

</div>

On Sat, 3 Mar 2007, Yannick G. wrote:

> but something isn’t working. I’m sure that there is a simple answer, but  
> I’m not that experienced with the language. If someone could please help  
> me out, I’d greatly appreciate it.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

## harp: ~\> ri String#each ------------------------------------------------------------ String#each str.each(separator=$/) {|substr| block } =\> str str.each\_line(separator=$/) {|substr| block } =\> str

```
  Splits _str_ using the supplied parameter as the record separator
  (+$/+ by default), passing each substring in turn to the supplied
  block. If a zero-length record separator is supplied, the string 

```

is  
split on +\n+ characters, except that multiple successive newlines  
are appended together.

```
     print "Example one\n"
     "hello\nworld".each {|s| p s}
     print "Example two\n"
     "hello\nworld".each('l') {|s| p s}
     print "Example three\n"
     "hello\n\n\nworld".each('') {|s| p s}

  _produces:_

     Example one
     "hello\n"
     "world"
     Example two
     "hel"
     "l"
     "o\nworl"
     "d"
     Example three
     "hello\n\n\n"
     "world"

```

harp:~ \> ruby -e’ puts String.instance\_methods.grep(/each/) ’  
each  
each\_with\_index  
each\_line  
each\_byte

harp:~ \> ruby -e’ “foobar”.each\_byte{|b| p b} ’  
102  
111  
111  
98  
97  
114

harp:~ \> ruby -e’ “foobar”.each\_byte{|b| p b.chr} ’  
“f”  
“o”  
“o”  
“b”  
“a”  
“r”

-a

---

<div class="post-metadata">

**Author:** ![Yannick\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@Yannick\_G](https://www.ruby-forum.com/u/Yannick_G)\
**Post date:** [March 3, 2007, 9:01am UTC](https://www.ruby-forum.com/t/string-each/87503/3 "2007-03-03T09:01:21Z")

</div>

On 3/3/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > str.each(separator=$/) {|substr| block } =\> str  
> > print “Example two\n”  
> > “hel”  
> > each\_with\_index  
> > 114
> 
> ## -a
> 
> be kind whenever possible… it is always possible.
> 
> - the dalai lama

I’d like to add two remarks  
(1) ruby -e’ “foobar”.split(“”).each{|b| p b} ’  
and  
(2) I feel it is a pity that  
s.each(“”) is not the same as s.split(“”).each  
and  
(3)  
“foobar”.to\_a does not deliver “foobar”.split(“”). The Arrayness of  
String might even indicate that String#to\_a return an array of bytes  
as delivered by #[index]?  
Note that the easiest way to do this ( which I found ) was

x=[]; each\_byte{ |b| x \<\< b}; x

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![Yannick\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@Yannick\_G](https://www.ruby-forum.com/u/Yannick_G)\
**Post date:** [March 3, 2007, 10:47am UTC](https://www.ruby-forum.com/t/string-each/87503/4 "2007-03-03T10:47:27Z")

</div>

On 3/3/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > String.each do  
> > > > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> > > > are appended together.  
> > > > Example one
> > > 
> > > 111  
> > > “b”
> 
> from breaking existing code).
> 
> irb(main):014:0\> require ‘enumerator’  
> =\> true  
> irb(main):015:0\> “foobar”.to\_enum(:each\_byte).to\_a  
> =\> [102, 111, 111, 98, 97, 114]

Thx Robert,  
when will I ever know the whole Standard API???

Robert

---

<div class="post-metadata">

**Author:** ![Yannick\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@Yannick\_G](https://www.ruby-forum.com/u/Yannick_G)\
**Post date:** [March 3, 2007, 10:40am UTC](https://www.ruby-forum.com/t/string-each/87503/5 "2007-03-03T10:40:26Z")

</div>

On 03.03.2007 09:00, Robert D. wrote:

> > > #block
> > 
> > ```
> > "hello\n"
> > 
> > ```
> > 
> > 111  
> > “a”  
> > I’d like to add two remarks  
> > (1) ruby -e’ “foobar”.split("").each{|b| p b} ’  
> > and  
> > (2) I feel it is a pity that  
> > s.each("") is not the same as s.split("").each  
> > and

Yeah, String’s enumeration is a bit weird and inconsistent. Using a  
String as array of lines does have it’s uses at times but I wonder  
whether changing #each to return characters would be more useful (apart  
from breaking existing code).

> (3)  
> “foobar”.to\_a does not deliver “foobar”.split(""). The Arrayness of  
> String might even indicate that String#to\_a return an array of bytes  
> as delivered by #[index]?  
> Note that the easiest way to do this ( which I found ) was
> 
> x=[]; each\_byte{ |b| x \<\< b}; x

There’s also

irb(main):014:0\> require ‘enumerator’  
=\> true  
irb(main):015:0\> “foobar”.to\_enum(:each\_byte).to\_a  
=\> [102, 111, 111, 98, 97, 114]

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Yannick\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@Yannick\_G](https://www.ruby-forum.com/u/Yannick_G)\
**Post date:** [March 3, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/string-each/87503/6 "2007-03-03T17:29:14Z")

</div>

On 3/3/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yeah, String’s enumeration is a bit weird and inconsistent. Using a  
> String as array of lines does have it’s uses at times but I wonder  
> whether changing #each to return characters would be more useful (apart  
> from breaking existing code).

Ruby 1.9 has added String#each\_char

## I agree, String’s enumeration is one of the most counterintuitive features for me in Ruby, but it is what it is.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Yannick\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d26b3c/32.png) [@Yannick\_G](https://www.ruby-forum.com/u/Yannick_G)\
**Post date:** [March 5, 2007, 9:00am UTC](https://www.ruby-forum.com/t/string-each/87503/7 "2007-03-05T09:00:46Z")

</div>

On 03.03.2007 10:46, Robert D. wrote:

> when will I ever know the whole Standard API???

Probably never. 🙂 Honestly, I don’t consider myself an expert in the  
whole standard lib API, but Enumerator is very useful - especially in  
combination with my beloved #inject. 🙂 But it took me quite some time  
to get aware of Enumerator, too. So nothing to worry I guess. 🙂

Kind regards

robert
