# Question about digits

**URL:** <https://www.ruby-forum.com/t/question-about-digits/83629>\
**Category:** Ruby\
**Created:** [January 28, 2007, 12:35am UTC](https://www.ruby-forum.com/t/question-about-digits/83629 "2007-01-28T00:35:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 28, 2007, 12:35am UTC](https://www.ruby-forum.com/t/question-about-digits/83629/1 "2007-01-28T00:35:53Z")

</div>

While playing a little with Ruby, I’ve been looking for a function  
each\_digit, or something similar, and I couldn’t find any (standard  
nor library). I think it’d be useful to have a function like that.  
It’s pretty simple to implement one for Integers

class Integer  
def each\_digit(base = 10, &block)  
return if zero?  
(self/base).each\_digit(base, &block)  
yield self % base  
end  
end

A first approach. Of course, it would be a little more complicated for  
negatives and Floats, specially dealing with precision.

What do you think?

---

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 28, 2007, 12:58am UTC](https://www.ruby-forum.com/t/question-about-digits/83629/2 "2007-01-28T00:58:55Z")

</div>

On 1/28/07, CHubas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> yield self % base
> end
> 
> ```
> 
> end
> 
> A first approach. Of course, it would be a little more complicated for  
> negatives and Floats, specially dealing with precision.
> 
> What do you think?

class Integer  
def each\_digit(base=10)  
to\_s( base ).each\_byte{ |b| yield b.chr }  
end #def each\_digit(base=10)  
end

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 28, 2007, 1:15am UTC](https://www.ruby-forum.com/t/question-about-digits/83629/3 "2007-01-28T01:15:56Z")

</div>

Why call them digits, if you can call them characters?

If you have got arbitrary\_number, you got arbitrary\_number.to\_s so:

* * *

## irb(main):001:0\> arbitrary\_number=123 =\> 123 irb(main):002:0\> arbitrary\_number.to\_s =\> “123” irb(main):002:0\> arbitrary\_number.to\_s.length =\> 3 irb(main):003:0\> (0…arbitrary\_number.to\_s.length).map{|digit| arbitrary\_number.to\_s.split(’’)[digit]} =\> [“1”,“2”,“3”]

This above will do for base 10, and printf stuff may help with hex,  
oct at least.

Remember it is ‘…’ and not ‘…’ so you don’t access  
arbitrary\_number[arbitrary\_number.length]

Hope this may help you

---

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 28, 2007, 1:03am UTC](https://www.ruby-forum.com/t/question-about-digits/83629/4 "2007-01-28T01:03:56Z")

</div>

On Jan 28, 2007, at 12:35 AM, CHubas wrote:

> ```
> end
> 
> ```
> 
> end
> 
> A first approach. Of course, it would be a little more complicated for  
> negatives and Floats, specially dealing with precision.

Good. I’d expect each\_digit to return strings though, since a digit  
is a symbol, not a number:

class Integer  
def each\_digit(base=10)  
abs.to\_s(base).each\_byte do |b|  
yield b.chr  
end  
end  
end

– fxn

---

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 28, 2007, 1:49am UTC](https://www.ruby-forum.com/t/question-about-digits/83629/5 "2007-01-28T01:49:59Z")

</div>

On 1/28/07, Xavier N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > return if zero?
> > 
> > ```
> 
> class Integer  
> def each\_digit(base=10, want\_sign=false)

```
       yield '-' if want_sign && self < 0 # to continue 

```

debugging  
my code;)

```
   abs.to_s(base).each_byte do |b|

```

---

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 28, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/question-about-digits/83629/6 "2007-01-28T14:25:26Z")

</div>

On Jan 28, 12:10 am, “Fer#” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Why call them digits, if you can call them characters?
> 
> If you have got arbitrary\_number, you got arbitrary\_number.to\_s so:  
> [… snip]  
> This above will do for base 10, and printf stuff may help with hex,  
> oct at least.

Actually, Fixnum#to\_s and Bignum#to\_s takes optional arguments  
specifying base, so to handle hex you’d do arbitrary\_number.to\_s(16)  
etc.

Vidar

---

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 29, 2007, 2:13am UTC](https://www.ruby-forum.com/t/question-about-digits/83629/7 "2007-01-29T02:13:30Z")

</div>

From: Fer# [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] :

# irb(main):003:0\> (0…arbitrary\_number.to\_s.length).map{|digit|

# arbitrary\_number.to\_s.split(‘’)[digit]}

# =\> [“1”,“2”,“3”]

forgive my ignorance, but why are you splitting them and rebuilding back  
the array. I mean why not,

# arbitrary\_number.to\_s.split ‘’

# =\> [“1”,“2”,“3”]

and it works for signed and float too

# arbitrary\_number=-123.45

=\> -123.45

# arbitrary\_number.to\_s.split ‘’

=\> [“-”, “1”, “2”, “3”, “.”, “4”, “5”]

kind regards -botp

---

<div class="post-metadata">

**Author:** ![chubas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a183cd/32.png) [@chubas](https://www.ruby-forum.com/u/chubas)\
**Post date:** [January 29, 2007, 8:36am UTC](https://www.ruby-forum.com/t/question-about-digits/83629/8 "2007-01-29T08:36:58Z")

</div>

On 1/29/07, PeÃ±a, Botp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # arbitrary\_number.to\_s.split ‘’
> 
> # =\> [“1”,“2”,“3”]
> 
> and it works for signed and float too
> 
> # arbitrary\_number=-123.45
> 
> =\> -123.45
> 
> # arbitrary\_number.to\_s.split ‘’
> 
> =\> [“-”, “1”, “2”, “3”, “.”, “4”, “5”]
> 
> kind regards -botp

that’s what we did, yielding as was required, no?  
Cheers  
Robert
