# How to read digits in a number

**URL:** <https://www.ruby-forum.com/t/how-to-read-digits-in-a-number/147782>\
**Category:** Ruby\
**Created:** [September 5, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/how-to-read-digits-in-a-number/147782 "2008-09-05T12:10:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [September 5, 2008, 12:10pm UTC](https://www.ruby-forum.com/t/how-to-read-digits-in-a-number/147782/1 "2008-09-05T12:10:33Z")

</div>

Hi all

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

thanks

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [September 5, 2008, 12:34pm UTC](https://www.ruby-forum.com/t/how-to-read-digits-in-a-number/147782/2 "2008-09-05T12:34:26Z")

</div>

On Fri, 05 Sep 2008 12:04:46 +0200, Pepe S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [September 5, 2008, 12:53pm UTC](https://www.ruby-forum.com/t/how-to-read-digits-in-a-number/147782/3 "2008-09-05T12:53:46Z")

</div>

2008/9/5 Josef ‘Jupp’ Schugt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Fri, 05 Sep 2008 12:04:46 +0200, Pepe S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![jsn1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7ea924/32.png) [@jsn1](https://www.ruby-forum.com/u/jsn1)\
**Post date:** [September 5, 2008, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-read-digits-in-a-number/147782/4 "2008-09-05T14:26:28Z")

</div>

On Fri, Sep 5, 2008 at 6:04 PM, Pepe S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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”]
