Convert to Roman numerals in Ruby Challenged by Sandy Metz's newsletter, I've tried to implement this kata from exercism. However, my solution, which passes the tests, is quite a lot simpler than Sandy's solution. Ok, so she's using refinements and also supports the inverse, but still... As she's probably smarter than me, do I miss something?

class Fixnum
  ROMAN = {
    1000 =&gt; "M",
    900 =&gt; "CM",
    500 =&gt; "D",
    400 =&gt; "CD",
    100 =&gt; "C",
    90 =&gt; "XC",
    50 =&gt; "L",
    40 =&gt; "XL",
    10 =&gt; "X",
    9 =&gt; "IX",
    5 =&gt; "V",
    4 =&gt; "IV",
    1 =&gt; "I"
  }

  def to_roman
    result = ""
    ROMAN.reduce(self) { |number, (divider, letter)|
      letter_multiplier, remainder = number.divmod(divider)
      result &lt;&lt; (letter * letter_multiplier)
      remainder
    }
    result
  end
end
I like your solution. You usually see it done with while/for loops that substract multipliers instead of taking the quotient/remainder. Some notes about your code:


ROMAN.reduce(self) { |number, (divider, letter)|. In a multiline block is idiomatic to use do/end.
Prefer building an array + join at the end, that appending to a string. Not that it matters with those sizes, but in general it's the recommended way (+= in a string is typically O(n^2), &lt;&lt; on an array is O(n)).
inject is a functional abstraction. While you can perform in-place updates within the block, it will break the assumption of many readers. One solution is to encapsulate the output within the state (using a hash, for example).


I'd write:

class Fixnum
  ROMAN = {...}

  def to_roman
    ROMAN.reduce(number: self, result: []) do |state, (divider, letter)|
      letter_multiplier, remainder = state[:number].divmod(divider)
      new_result = state[:result] + [letter * letter_multiplier]
      {number: remainder, result: new_result}
    end[:result].join
  end
end


Note that this is a state machine: it iterates over an input with an initial state, on each loop the state is updated, and the result is the final state.