Robert Dober
3/21/2006 9:48:00 AM
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