[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: iterate chars in a string

Berger, Daniel

3/20/2006 7:52:00 PM

> -----Original Message-----
> From: Mike Austin [mailto:noone@nowhere.com]
> Sent: Monday, March 20, 2006 12:39 PM
> To: ruby-talk ML
> Subject: Re: iterate chars in a string
>
>
> "I am puzzled".each_byte { |b| puts b.chr }
>
> I'm surprised that not many people knew about 'each_byte()'.
> Maybe it's a
> problem with Ruby docs? Or maybe it is just
> counter-intuitive - I would expect
> each() iterate over bytes, and provide each_lines() to
> iterate over lines instead.
>
> Mike
>
> http://www.rubycentra...

Modifying String#to_a to return an array of characters has been brought
up before (ruby-talk:148588 and following). I don't think Matz likes
the idea, though.

Dan


14 Answers

Robert Dober

3/20/2006 9:27:00 PM

0

Mike I really agree, I *was* expecting that behavior from "each" too, some
time ago, and it took me some time to figure it out
BTW I think the "nicest" solutions is mine combined with Robert's (pun
intended)

"Ty Mr. Klemme".each_byte { |b| puts b.chr }

my "%" stuff was rather clumsy.

bye for now
Robert

On 3/20/06, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
>
> > -----Original Message-----
> > From: Mike Austin [mailto:noone@nowhere.com]
> > Sent: Monday, March 20, 2006 12:39 PM
> > To: ruby-talk ML
> > Subject: Re: iterate chars in a string
> >
> >
> > "I am puzzled".each_byte { |b| puts b.chr }
> >
> > I'm surprised that not many people knew about 'each_byte()'.
> > Maybe it's a
> > problem with Ruby docs? Or maybe it is just
> > counter-intuitive - I would expect
> > each() iterate over bytes, and provide each_lines() to
> > iterate over lines instead.
> >
> > Mike
> >
> > http://www.rubycentra...
>
> Modifying String#to_a to return an array of characters has been brought
> up before (ruby-talk:148588 and following). I don't think Matz likes
> the idea, though.
>
> Dan
>
>


--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

jogloran

3/21/2006 7:32:00 AM

0

The thing I don't like about this behaviour is that an algorithm which
operates on containers and expects #each can't work immediately with
strings.

Daniel Tse

On 3/21/06, Robert Dober <robert.dober@gmail.com> wrote:
>
> Mike I really agree, I *was* expecting that behavior from "each" too, some
> time ago, and it took me some time to figure it out
> BTW I think the "nicest" solutions is mine combined with Robert's (pun
> intended)
>
> "Ty Mr. Klemme".each_byte { |b| puts b.chr }
>
> my "%" stuff was rather clumsy.
>
> bye for now
> Robert
>
> On 3/20/06, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
> >
> > > -----Original Message-----
> > > From: Mike Austin [mailto:noone@nowhere.com]
> > > Sent: Monday, March 20, 2006 12:39 PM
> > > To: ruby-talk ML
> > > Subject: Re: iterate chars in a string
> > >
> > >
> > > "I am puzzled".each_byte { |b| puts b.chr }
> > >
> > > I'm surprised that not many people knew about 'each_byte()'.
> > > Maybe it's a
> > > problem with Ruby docs? Or maybe it is just
> > > counter-intuitive - I would expect
> > > each() iterate over bytes, and provide each_lines() to
> > > iterate over lines instead.
> > >
> > > Mike
> > >
> > > http://www.rubycentra...
> >
> > Modifying String#to_a to return an array of characters has been brought
> > up before (ruby-talk:148588 and following). I don't think Matz likes
> > the idea, though.
> >
> > Dan
> >
> >
>
>
> --
> Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
> concerne l'univers, je n'en ai pas acquis la certitude absolue.
>
> - Albert Einstein
>
>

Robert Klemme

3/21/2006 8:57:00 AM

0

jogloran wrote:
> The thing I don't like about this behaviour is that an algorithm which
> operates on containers and expects #each can't work immediately with
> strings.

That's not true. It just depends on what you consider to be the parts
of a string. I'd agree that naturally one would expect characters to be
that - but "lines" is another option. And that's the one that has been
chose by Matz. It has some advantages, too, e.g. if you slurp in a file
into a single string and then want to iterate the lines.

Kind regards

robert

Robert Dober

3/21/2006 9:09:00 AM

0

Would it not be nice to simply extend the behavior of String#each e.g. like
this

"Really nothing intelligent to tell".each <some_intelligent_choice> do
|c|
puts c
end
==>
R
e
etc. etc.

<some_intelligent_choice> might be 1 (imagine what n could do!)
[ we could even formulate endless loops like this
"".each 0 do
puts "Eternity is a hack of a long time"
end
that is *really* great ;)
]
or shall everybody do it oneself

class String
def my_iterator ....

I just do not think so.

Cheers
Robert

On 3/21/06, Robert Klemme <bob.news@gmx.net> wrote:
>
> jogloran wrote:
> > The thing I don't like about this behaviour is that an algorithm which
> > operates on containers and expects #each can't work immediately with
> > strings.
>
> That's not true. It just depends on what you consider to be the parts
> of a string. I'd agree that naturally one would expect characters to be
> that - but "lines" is another option. And that's the one that has been
> chose by Matz. It has some advantages, too, e.g. if you slurp in a file
> into a single string and then want to iterate the lines.
>
> Kind regards
>
> robert
>
>


--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

Mauricio Fernández

3/21/2006 9:25:00 AM

0

On Tue, Mar 21, 2006 at 05:58:50PM +0900, Robert Klemme wrote:
> jogloran wrote:
> >The thing I don't like about this behaviour is that an algorithm which
> >operates on containers and expects #each can't work immediately with
> >strings.
>
> That's not true. It just depends on what you consider to be the parts
> of a string. I'd agree that naturally one would expect characters to be
> that - but "lines" is another option. And that's the one that has been
> chose by Matz. It has some advantages, too, e.g. if you slurp in a file
> into a single string and then want to iterate the lines.

Besides,

RUBY_VERSION # => "1.8.4"
require 'enumerator'

def foo(x)
x.each{|y| p y.chr}
end

str = "foo bar baz"
foo(str.enum_for(:each_byte))
# >> "f"
# >> "o"
# >> "o"
# >> " "
# >> "b"
# >> "a"
# >> "r"
# >> " "
# >> "b"
# >> "a"
# >> "z"

--
Mauricio Fernandez - http://eige... - singular Ruby


Robert Klemme

3/21/2006 9:30:00 AM

0

Robert Dober wrote:

Please don't top post.

> Would it not be nice to simply extend the behavior of String#each e.g. like
> this
>
> "Really nothing intelligent to tell".each <some_intelligent_choice> do
> |c|
> puts c
> end
> ==>
> R
> e
> etc. etc.
>
> <some_intelligent_choice> might be 1 (imagine what n could do!)

We have that already: it's Enumerator.

irb(main):003:0> require 'enumerator'
=> true
irb(main):004:0> "foo\nbar".to_enum(:each_byte).each {|c| puts c.chr}
f
o
o

b
a
r
=> "foo\nbar"
irb(main):005:0> "foo\nbar".to_enum(:scan, /./m).each {|c| puts c}
f
o
o

b
a
r
=> "foo\nbar"

Cheers

robert

Robert Dober

3/21/2006 9:48:00 AM

0

Sorry Robert but I do not think I made myself too clear

we now about each_byte and I already combined your #chr and String#each_byte

so we had already established

"Ty Mr. Klemme".each_byte { |b| puts b.chr }

which is pretty elegant, don't you agree?
So Enumerators are not needed.

Now after that we switched discussing the behaviour of String#each and I
*really* feal that String#each should us give that kind of behaviour.
As I see that a lot of people think that the current behaviour of
String#each is a good one, and changing it would not be an option anyway I
thaught that the only solution would be to enhance the behaviour of
String#each.
The idea for that behaviour comes from Ruby itself (look at IO#gets)

then

"Ty Mr. Klemme".each_byte { |b| puts b.chr }

would be the same as

"Ty Mr. Klemme".each( 1 ) { |b| puts b }

There is always more than one way to do it ;)

Robert


On 3/21/06, Robert Klemme <bob.news@gmx.net> wrote:
>
> Robert Dober wrote:
>
> Please don't top post.
>
> > Would it not be nice to simply extend the behavior of String#each e.g.
> like
> > this
> >
> > "Really nothing intelligent to tell".each <some_intelligent_choice> do
> > |c|
> > puts c
> > end
> > ==>
> > R
> > e
> > etc. etc.
> >
> > <some_intelligent_choice> might be 1 (imagine what n could do!)
>
> We have that already: it's Enumerator.
>
> irb(main):003:0> require 'enumerator'
> => true
> irb(main):004:0> "foo\nbar".to_enum(:each_byte).each {|c| puts c.chr}
> f
> o
> o
>
> b
> a
> r
> => "foo\nbar"
> irb(main):005:0> "foo\nbar".to_enum(:scan, /./m).each {|c| puts c}
> f
> o
> o
>
> b
> a
> r
> => "foo\nbar"
>
> Cheers
>
> robert
>
>


--
Deux choses sont infinies : l'univers et la bêtise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.

- Albert Einstein

Gregory Seidman

3/21/2006 12:28:00 PM

0

On Tue, Mar 21, 2006 at 04:31:39PM +0900, jogloran wrote:
} The thing I don't like about this behaviour is that an algorithm which
} operates on containers and expects #each can't work immediately with
} strings.

A decision had to be made on how to split up a string when using each. The
decision was made to split it up by lines, since that was deemed to be the
most often used case. I think that's probably a correct assessment. Now, if
you want it to use something other than newlines as its split, you can
explicitly split by whatever you want:

'foobar'.split('').each { |c| puts c }

Basically, stringvar.each is a shortcut for stringvar.split("\n").each
because line splitting is the common case.

} Daniel Tse
--Greg

} On 3/21/06, Robert Dober <robert.dober@gmail.com> wrote:
} >
} > Mike I really agree, I *was* expecting that behavior from "each" too, some
} > time ago, and it took me some time to figure it out
} > BTW I think the "nicest" solutions is mine combined with Robert's (pun
} > intended)
} >
} > "Ty Mr. Klemme".each_byte { |b| puts b.chr }
} >
} > my "%" stuff was rather clumsy.
} >
} > bye for now
} > Robert
} >
} > On 3/20/06, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
} > >
} > > > -----Original Message-----
} > > > From: Mike Austin [mailto:noone@nowhere.com]
} > > > Sent: Monday, March 20, 2006 12:39 PM
} > > > To: ruby-talk ML
} > > > Subject: Re: iterate chars in a string
} > > >
} > > >
} > > > "I am puzzled".each_byte { |b| puts b.chr }
} > > >
} > > > I'm surprised that not many people knew about 'each_byte()'.
} > > > Maybe it's a
} > > > problem with Ruby docs? Or maybe it is just
} > > > counter-intuitive - I would expect
} > > > each() iterate over bytes, and provide each_lines() to
} > > > iterate over lines instead.
} > > >
} > > > Mike
} > > >
} > > > http://www.rubycentra...
} > >
} > > Modifying String#to_a to return an array of characters has been brought
} > > up before (ruby-talk:148588 and following). I don't think Matz likes
} > > the idea, though.
} > >
} > > Dan
} > >
} > >
} >
} >
} > --
} > Deux choses sont infinies : l'univers et la b?tise humaine ; en ce qui
} > concerne l'univers, je n'en ai pas acquis la certitude absolue.
} >
} > - Albert Einstein
} >
} >


Trans

3/21/2006 12:53:00 PM

0

<i>Modifying String#to_a to return an array of characters has been
brought
up before (ruby-talk:148588 and following). I don't think Matz likes
the idea, though.</i>

It should be pointed out that there really is a flaw in the way Ruby
handles this. #each takes a parameter which defaults to the global
variable $/ to determine the actual split to perform. Not only can't
enumerable handle the parameter, but worse relying on a globabl
varaible like that is dangerous! To be safe one would have to set the
global everytime it is used, or aways give the parameter. Otherwise
another lib might change it on you. The upshot of all this is that we
are much less inclined to even bother with String#each, which is too
bad.

BTW, see Calibre's EnumerablePass for a way to allow #each to take
parameters and still have enumerablity.

T.

dblack

3/21/2006 1:17:00 PM

0