[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

how to read digits in a number

Pepe Sanchez

9/5/2008 10:05:00 AM

Hi all

I would like to read all the characters in a string. For example
"123456" I need to read every digit separately.

thanks
--
Posted via http://www.ruby-....

3 Answers

Josef 'Jupp' Schugt

9/5/2008 10:28:00 AM

0

On Fri, 05 Sep 2008 12:04:46 +0200, Pepe Sanchez <jsnit@jsnit.com> wrote:

> I would like to read all the characters in a string. For example
> "123456" I need to read every digit separately.

For strings use

"123456".split(//)

for integers

"123456".split(//).map{|x| x.to_i}

HTH,

Josef 'Jupp' Schugt
--
Blog: http://penpen.gooda...
PGP key (id 6CC6574F): http://wwwkeys.d...
Jabber - http://www.j... - contact information on request

Robert Klemme

9/5/2008 10:47:00 AM

0

2008/9/5 Josef 'Jupp' Schugt <jupp@gmx.de>:
> On Fri, 05 Sep 2008 12:04:46 +0200, Pepe Sanchez <jsnit@jsnit.com> wrote:
>
>> I would like to read all the characters in a string.

For me, glasses work extremely well. ;-)

> For example
>> "123456" I need to read every digit separately.
>
> For strings use
>
> "123456".split(//)

Or

"12345".scan /./m do |s|
...
end

Cheers

robert


--
use.inject do |as, often| as.you_can - without end

botp

9/5/2008 12:20:00 PM

0

On Fri, Sep 5, 2008 at 6:04 PM, Pepe Sanchez <jsnit@jsnit.com> wrote:
> I would like to read all the characters in a string. For example
> "123456" I need to read every digit separately.

> "1234".each_char{|x| p x}
"1"
"2"
"3"
"4"
=> "1234"

> "1234".each_char.map
=> ["1", "2", "3", "4"]