Question about digits

Discussion in 'Ruby' started by CHubas, Jan 27, 2007.

  1. CHubas

    CHubas Guest

    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?
     
    CHubas, Jan 27, 2007
    #1
    1. Advertising

  2. CHubas

    Xavier Noria Guest

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

    > 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.


    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
     
    Xavier Noria, Jan 28, 2007
    #2
    1. Advertising

  3. CHubas

    Fer# Guest

    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
     
    Fer#, Jan 28, 2007
    #3
  4. On Jan 28, 12:10 am, "Fer#" <> 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
     
    Vidar Hokstad, Jan 28, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jasper
    Replies:
    1
    Views:
    452
    Joe Smith
    Jun 27, 2004
  2. RSB
    Replies:
    7
    Views:
    32,159
    Hannes Decorte
    Jun 14, 2004
  3. Keith Valentine
    Replies:
    5
    Views:
    18,525
    Keith Valentine
    Sep 20, 2004
  4. aefxx
    Replies:
    1
    Views:
    593
    Virgil Green
    Dec 17, 2004
  5. Karen
    Replies:
    5
    Views:
    112
    HolaGoogle
    Sep 26, 2003
Loading...

Share This Page