String.each

Y

Yannick Grams

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

ara.t.howard

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.

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
 
R

Robert Dober

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

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
 
R

Robert Klemme

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
 
R

Robert Dober

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]
Thx Robert,
when will I ever know the whole Standard API???

Robert
 
R

Rick DeNatale

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

Robert Klemme

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,235
Messages
2,571,181
Members
47,817
Latest member
BartHeckma

Latest Threads

Top