Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Question about digits

Reply
Thread Tools

Question about digits

 
 
CHubas
Guest
Posts: n/a
 
      01-27-2007
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?

 
Reply With Quote
 
 
 
 
Xavier Noria
Guest
Posts: n/a
 
      01-28-2007
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




 
Reply With Quote
 
 
 
 
Fer#
Guest
Posts: n/a
 
      01-28-2007
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

 
Reply With Quote
 
Vidar Hokstad
Guest
Posts: n/a
 
      01-28-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with PRI/ISDN recv'ed digits routing A Dickson Cisco 12 07-09-2008 02:18 PM
'long' integer of 19 digits will not compile Keith Valentine Java 5 09-20-2004 02:22 AM
Need > 15 digits precision without big performance hit Jasper Perl 1 06-27-2004 08:25 AM
how to validate a textBox for 0-9 Digits only RSB ASP .Net 7 06-14-2004 06:50 PM
Question on Digits and Special Characters Karen ASP General 5 09-26-2003 06:28 PM



Advertisments