# (mandriva,eclipse) extract digits from fixnums

**URL:** <https://www.ruby-forum.com/t/mandriva-eclipse-extract-digits-from-fixnums/166880>\
**Category:** Ruby\
**Created:** [April 30, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/mandriva-eclipse-extract-digits-from-fixnums/166880 "2009-04-30T13:55:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jolivierld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jolivierld](https://www.ruby-forum.com/u/jolivierld)\
**Post date:** [April 30, 2009, 1:55pm UTC](https://www.ruby-forum.com/t/mandriva-eclipse-extract-digits-from-fixnums/166880/1 "2009-04-30T13:55:12Z")

</div>

hi,

I want to do a basic thing but without succes:  
if a=1232, let nb\_nb=4 (the number of digits)  
for i from 1 to nb\_nb, I want the following results:  
i=1 =\> res=1 (first digit of a) and NOT the number 01 (with a zero in  
front of the 1)  
i=2 =\> res=2 (same thing)  
i=3 =\> res=3  
i=4 =\> res=2

I tried a few things, like convert the number in array (it seems to keep  
the “0” in the result)…

do you have a solution?

olivier.

* * *

Yahoo! Mail réinvente le mail ! Découvrez le nouveau Yahoo! Mail et son  
interface révolutionnaire.

> **[Yahoo](https://mail.yahoo.com/)**
>
> It's

---

<div class="post-metadata">

**Author:** ![jolivierld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jolivierld](https://www.ruby-forum.com/u/jolivierld)\
**Post date:** [April 30, 2009, 2:38pm UTC](https://www.ruby-forum.com/t/mandriva-eclipse-extract-digits-from-fixnums/166880/2 "2009-04-30T14:38:44Z")

</div>

You could try

a.to\_s.split(//).collect {|s| s.to\_i}

and extract nb\_nb elements.

Bob Schaaf

---

<div class="post-metadata">

**Author:** ![jolivierld](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/34f0e0/32.png) [@jolivierld](https://www.ruby-forum.com/u/jolivierld)\
**Post date:** [April 30, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/mandriva-eclipse-extract-digits-from-fixnums/166880/3 "2009-04-30T14:58:21Z")

</div>

Olivier L. wrote:

> hi,
> 
> I want to do a basic thing but without succes:  
> if a=1232, let nb\_nb=4 (the number of digits)  
> for i from 1 to nb\_nb, I want the following results:  
> i=1 =\> res=1 (first digit of a) and NOT the number 01 (with a zero in  
> front of the 1)  
> i=2 =\> res=2 (same thing)  
> i=3 =\> res=3  
> i=4 =\> res=2
> 
> I tried a few things, like convert the number in array (it seems to keep  
> the “0” in the result)…
> 
> do you have a solution?

It’s not clear to me what you want to do, but maybe this will help:

a=1232  
str = a.to\_s  
p str

–output:–  
“1232”

digits = []  
str.length.times do |i|  
digits \<\< str[i, 1]  
end

p digits

–output:–  
[“1”, “2”, “3”, “2”]

> res=1 (first digit of a) and NOT the number 01

1 and 01 are the same _number_.
