Skip to content

Instantly share code, notes, and snippets.

View lantoli's full-sized avatar

Leo Antoli lantoli

View GitHub Profile
@lantoli
lantoli / roman_numerals.rb
Created January 17, 2011 13:14
roman numerals in ruby using inject
require 'rspec'
class Fixnum
ROMANS = { M: 1000, CM: 900, D: 500, CD: 400, C: 100, XC: 90, L: 50, XL: 40, X: 10, IX: 9, V: 5, IV: 4, I: 1 }
def to_roman
remaining_number = self
ROMANS.inject ("") do | roman_str, current_number |
times,remaining_number = remaining_number.divmod current_number[1]
roman_str + current_number[0].to_s * times