[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String.each

Yannick Grams

3/3/2007 3:36:00 AM

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

9 Answers

Ara.T.Howard

3/3/2007 3:42:00 AM

0

Robert Dober

3/3/2007 8:00:00 AM

0

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


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

Robert Klemme

3/3/2007 9:38:00 AM

0

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

Robert Dober

3/3/2007 9:47:00 AM

0

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

Rick DeNatale

3/3/2007 4:29:00 PM

0

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

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

Robert Klemme

3/5/2007 7:59:00 AM

0

On 03.03.2007 10:46, Robert Dober 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

The Revd

3/12/2013 4:21:00 PM

0

On Tue, 12 Mar 2013 17:06:18 +0100, "Heinrich"
<Heinrich@Ruhrgasnet.de> wrote:

>
>
>"NEMO" schreef in bericht
>news:ddc26b03-77b4-4fa0-8b00-3455141c4ffc@o5g2000vbp.googlegroups.com...
>
>You forgot to add that we punch arseholes in the face after telling
>them to fuck off!
>
>i noticed that canada has the same imbecilic tipping system as in the united
>states

jewbaited jew wog Lambsky would shit himself if anyone said BOO to
him!

brianlambsbigtoe

3/12/2013 4:21:00 PM

0

Why does a dog lick it's balls? Because it can!

Why does Pig Boy/The Rev'd lick Heinrich's balls? Because the dog
won't!

"Slurp - slurp, lick - lick, lick - lick, slurp - slurp!"

The Peeler

3/12/2013 7:07:00 PM

0

On Tue, 12 Mar 2013 17:06:18 +0100, Dumb Heini, the heavily medicated Dutch
resident Nazi troll and laughing stock of sci and scj, wrote:

> You forgot to add that we punch arseholes in the face after telling
> them to fuck off!
>
> i noticed that canada has the same imbecilic tipping system as in the united
> states

Talking of tips, how much pocket money do you get from your wife a day, you
lazy layabout? LOL

--
Dumb Heini admits to living off his wife:
"i told you before my wife and i swapped jobs so i stay at home to do the
household and she is going to the office"
MID: <h1Ybs.3823$EP3.1211@newsfe07.iad>