String.each

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.

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

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

On 3/3/07, [email protected] [email protected] 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

On 3/3/07, Robert K. [email protected] wrote:

String.each do
Posted via 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

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

On 3/3/07, Robert K. [email protected] 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/

On 03.03.2007 10:46, Robert D. wrote:

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

Probably never. :slight_smile: 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. :slight_smile: But it took me quite some time
to get aware of Enumerator, too. So nothing to worry I guess. :slight_smile:

Kind regards

robert