lonewarrior556
lonewarrior556

Reputation: 4479

How to write a method that works for all numbers

I'm writing a Fixnum class method to_words that takes any number and translates it to English so,

2.to_words
#=> "two"
2030.to_words
#=> "two thousand thirty" 

I would like it to handle all numbers, and there a problem once I get a little past 1 billion:

1000002000.to_words
#=> "one billion two thousand"
1074000000.to_words
#=> NoMethodError
1074000000.class
#=> Bignum

Is there a way to extend my Fixnum.to_words method to Bignum?

Upvotes: 1

Views: 61

Answers (1)

ptd
ptd

Reputation: 3053

Both Fixnum and Bignum inherit from Integer, so it would be preferable in your case to define #to_words on Integer so either Fixnums or Bignums would inherit that method.

Upvotes: 5

Related Questions