Permalink
Browse files

Refactored to increase readability

  • Loading branch information...
1 parent 571ef09 commit 0bdc2206577a013094f43b60b6048d9c1251c542 @adarsh committed Apr 24, 2012
Showing with 12 additions and 11 deletions.
  1. +12 −11 roman_numerals.rb
View
@@ -17,23 +17,24 @@ class RomanNumerals
'I' => 1 }
def initialize(n)
- @n = n
+ @arabic = n
end
def to_roman
- roman = ROMAN_ARABIC_MAP.map do |roman_numeral, arabic_numeral|
- roman_numeral * roman_letter_multiplier(arabic_numeral)
- end
+ roman_output = ""
- roman.join('')
- end
+ ROMAN_ARABIC_MAP.map do |roman_numeral, arabic_numeral|
+ division_result_rounded = @arabic / arabic_numeral
- private
+ if division_result_rounded > 0
+ roman_letters_added = roman_numeral * division_result_rounded
+ roman_output << roman_letters_added
+ decrement_remaining_arabic_value = division_result_rounded * arabic_numeral
+ @arabic -= decrement_remaining_arabic_value
+ end
+ end
- def roman_letter_multiplier(arabic_numeral)
- n_times = @n / arabic_numeral
- @n -= (n_times * arabic_numeral)
- n_times
+ roman_output
end
end

0 comments on commit 0bdc220

Please sign in to comment.