Forum: Ruby Question about digits

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F2c730957ef0aec5616091b30fe88075?d=identicon&s=25 CHubas (Guest)
on 2007-01-28 00:35
(Received via mailing list)
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?
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-01-28 00:58
(Received via mailing list)
On 1/28/07, CHubas <CHubas7@gmail.com> 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
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2007-01-28 01:03
(Received via mailing list)
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
3af7064fc6d5764a7855ef799687fd7c?d=identicon&s=25 Fer# (Guest)
on 2007-01-28 01:15
(Received via mailing list)
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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-01-28 01:49
(Received via mailing list)
On 1/28/07, Xavier Noria <fxn@hashref.com> 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|
Caf3d97ceff60d6d105c45305d34658c?d=identicon&s=25 Vidar Hokstad (Guest)
on 2007-01-28 14:25
(Received via mailing list)
On Jan 28, 12:10 am, "Fer#" <fernando.mdelacu...@gmail.com> 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
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2007-01-29 02:13
(Received via mailing list)
From: Fer# [mailto:fernando.mdelacueva@gmail.com] :

# 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
703fbc991fd63e0e1db54dca9ea31b53?d=identicon&s=25 Robert Dober (Guest)
on 2007-01-29 08:36
(Received via mailing list)
On 1/29/07, Peña, Botp <botp@delmonte-phil.com> 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
This topic is locked and can not be replied to.