7

I have written some general functions to convert between decimal and any other base-n number system(n<=36 for now) and vice-versa. Don't want to make things messy here so i have posted the code here.

Could anybody suggest any better way for this? May be more effective and Rubyish?

Thanks

RubyDubee
  • 2,426
  • 2
  • 23
  • 34

2 Answers2

13

There's already the to_s method on Numeric and the to_i method on String to convert back:

irb(main):013:0> 10.to_s(36)
=> "a"
irb(main):014:0> "a".to_i(36)
=> 10
Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • +1 - Wow!!.. i didn't see it... that's amazing... thanks. But i wasn't looking for that... I was just trying my hands on some usual problems. Do you have any suggestions for refactoring it? – RubyDubee Mar 28 '10 at 10:13
  • 5001.to_s(36).to_i(10) => 3 – Mori Nov 14 '13 at 02:50
0

Check out Rosetta Code: http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby

glenn jackman
  • 238,783
  • 38
  • 220
  • 352