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.

···

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

···

On Sat, 3 Mar 2007, Yannick Grams wrote:

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.

--
Posted via http://www.ruby-forum.com/\.

------------------------------------------------------------------------
      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
98
97
114

harp:~ > ruby -e' "foobar".each_byte{|b| p b.chr} '
"f"
"o"
"b"
"a"
"r"

-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, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Sat, 3 Mar 2007, Yannick Grams wrote:

> 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.
>
> --
> 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
98
97
114

harp:~ > ruby -e' "foobar".each_byte{|b| p b.chr} '
"f"
"o"
"b"
"a"
"r"

-a
--
be kind whenever possible... it is always possible.
- the dalai lama

--
There are three kinds of people, those who can count and those who can't.

> 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.
>
> --
> 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
98
97
114

harp:~ > ruby -e' "foobar".each_byte{|b| p b.chr} '
"f"
"o"
"b"
"a"
"r"

-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

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 03.03.2007 09:00, Robert Dober wrote:

On 3/3/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:

On Sat, 3 Mar 2007, Yannick Grams wrote:

>>
>> > 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.
>> >
>> > --
>> > 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
>> 98
>> 97
>> 114
>>
>> harp:~ > ruby -e' "foobar".each_byte{|b| p b.chr} '
>> "f"
>> "o"
>> "b"
>> "a"
>> "r"
>>
>> -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

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

···

On 3/3/07, Robert Klemme <shortcutter@googlemail.com> wrote:

On 03.03.2007 09:00, Robert Dober wrote:
> On 3/3/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
>> On Sat, 3 Mar 2007, Yannick Grams wrote:
Kind regards

        robert

--
We have not succeeded in answering all of our questions.
In fact, in some ways, we are more confused than ever.
But we feel we are confused on a higher level and about more important things.
-Anonymous

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.

···

On 3/3/07, Robert Klemme <shortcutter@googlemail.com> 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).

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

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

···

On 03.03.2007 10:46, Robert Dober wrote:

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