[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

adding anotehr each method to String

unbewusst.sein

3/25/2007 8:20:00 AM

f it doesn't exist allready, i'd like to add another each method to
String similar to each_byte except it loops over chars for example :

"zero".each_char{|c| puts c}

would print-out :
z
e
r
o

i know how to add a new method to a given class, what i don't know is
specifically for methods like each which pass an arg to a given block...


--
It's easy to play any musical instrument: all you have to do is
touch the right key at the right time and the instrument will
play itself. -- J.S. Bach
5 Answers

Chris Shea

3/25/2007 7:41:00 AM

0

On Mar 25, 2:20 am, unbewusst.s...@wortanschahung.com.invalid (Une
Bévue) wrote:
> f it doesn't exist allready, i'd like to add another each method to
> String similar to each_byte except it loops over chars for example :
>
> "zero".each_char{|c| puts c}
>
> would print-out :
> z
> e
> r
> o
>
> i know how to add a new method to a given class, what i don't know is
> specifically for methods like each which pass an arg to a given block...
>
> --
> It's easy to play any musical instrument: all you have to do is
> touch the right key at the right time and the instrument will
> play itself. -- J.S. Bach

What you need is "yield".

This does what you need:

class String
def each_char
0.upto(self.length - 1) do |x|
yield self[x,1]
end
end
end

yield passes a value to a block.

John Joyce

3/25/2007 8:01:00 AM

0

Might not do what you expect.
under current conditions you might get:
¨
u

instead of
ü


On Mar 25, 2007, at 4:25 PM, Une Bévue wrote:

> f it doesn't exist allready, i'd like to add another each method to
> String similar to each_byte except it loops over chars for example :
>
> "zero".each_char{|c| puts c}
>
> would print-out :
> z
> e
> r
> o
>
> i know how to add a new method to a given class, what i don't know is
> specifically for methods like each which pass an arg to a given
> block...
>
>
> --
> It's easy to play any musical instrument: all you have to do is
> touch the right key at the right time and the instrument will
> play itself. -- J.S. Bach
>


Bertram Scharpf

3/25/2007 5:34:00 PM

0

Hi,

Am Sonntag, 25. Mär 2007, 16:25:05 +0900 schrieb Une Bévue:
> "zero".each_char{|c| puts c}
>
> would print-out :
> z
> e
> r
> o

The split method was already proposed.

"another".split //
"another".split ""
"another".scan /./
"änöthér".scan /./u # will preserve UTF-8 characters

"another".each_byte { |x| c = x.chr ; ... }
"another".unpack( "C*")
"änöthér".unpack( "U*")

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

James Gray

3/26/2007 5:38:00 PM

0

On Mar 25, 2007, at 2:25 AM, Une Bévue wrote:

> f it doesn't exist allready, i'd like to add another each method to
> String similar to each_byte except it loops over chars for example :
>
> "zero".each_char{|c| puts c}
>
> would print-out :
> z
> e
> r
> o

#!/usr/bin/env ruby -wKU

require "jcode"

"zero".each_char { |chr| p chr }
# >> "z"
# >> "e"
# >> "r"
# >> "o"

__END__

James Edward Gray II

unbewusst.sein

3/26/2007 6:25:00 PM

0

James Edward Gray II <james@grayproductions.net> wrote:

>
> #!/usr/bin/env ruby -wKU
>
> require "jcode"
>
> "zero".each_char { |chr| p chr }
> # >> "z"
> # >> "e"
> # >> "r"
> # >> "o"
>
> __END__

thanks to all !


--
Une Bévue