* [Roman Numerals Kata](http://exercism.io/exercises/ruby/roman-numerals/readme) * Things that I applied or learnt along the journey that led to the final solution - * **Approach the problem in small steps.** I was solving this kata for the first time, I didn't know of any pattern whatsoever initially, hence took this problem, one step at a time and it eventually helped me get to solution. Often times one worries about the end solution or just the destination, without traveling the journey bit by bit. * I know you may think this approach(solving for one test at a time as done in [this commit](https://github.com/boddhisattva/exercism/commit/fd780903ee16970179ea6360623d6af740d3760d) and [this one](https://github.com/boddhisattva/exercism/commit/678b16d2b7c91d8dda9aece7ccd6468936d7d82b)) wouldn't give you the desired solution that'll satisfy all cases, but trust me it will help you eventually get there. That's why this approach is worth it. * Used the older hash syntax in ruby so that I could leverage keys as strings. With this I could do away with the `to_s` that I was using [here](https://github.com/boddhisattva/exercism/commit/969b0280d662a0d8467fdbab0e34fbb1ddbbbea7). * Learnt that I don’t have to use a more higher order class like the `Numeric` class. Instead, I could use `Fixnum`. I also learnt about the hierarchy of these classes. `Numeric` is the parent of `Integer` which in turn is the parent of `Fixnum`. * Learnt that **`reverse_each` is much faster than using `reverse.each`** and made the relevant refactoring [here](https://github.com/boddhisattva/exercism/commit/5fba273bcd12a4c8e7accd5a7f4e577ce95a820d) * **Refactoring related learnings** * When I was looking at other people's solution like that of [rwz's](http://exercism.io/submissions/61ad4a5a70ab4194aaf09875997da322), I realized I can define my `ROMAN_NUMERALS` related hash in the reverse order [like what was done here](https://github.com/boddhisattva/exercism/commit/d5199b0fe7a3b0705186897cf4f9dddb120e254b) and thereby I wouldn't even need to use the `reverse_each` method, saving me an additional computation. * I learnt that I could further simplify my solution after referring to [rwz's solution](http://exercism.io/submissions/61ad4a5a70ab4194aaf09875997da322). The `if...else` related branches that I had introduced to handle the [subtractive notation related to roman numerals](https://en.wikipedia.org/wiki/Subtractive_notation) can be dealt without using that amount of branching. I made the relevant changes wrt this learning [in this commit](https://github.com/boddhisattva/exercism/commit/c76e1cf286c9cc0ba5b31adda53cb294d4af8905). * Learnt how we could use `each_with_object` to do away with a temp variable from [henrik's solution](http://exercism.io/submissions/525704159fcfeabc2200023a). Appropriate [change was made in this commit](https://github.com/boddhisattva/exercism/commit/01e767759ce8ef952739e9cdcf895f70ce7e553b). Below is how the code looks before and after using `each_with_object` **Before** ```ruby def to_roman num = '' number_to_convert = self ROMAN_NUMERALS.each do |roman_numeral, numeric_equivalent| next if numeric_equivalent > number_to_convert quotient, remainder = number_to_convert.divmod(numeric_equivalent) number_to_convert = remainder num << roman_numeral * quotient end num end ``` **After** ```ruby def to_roman number_to_convert = self ROMAN_NUMERALS.each_with_object("") do |(roman_numeral, numeric_equivalent), roman_equivalent| next if numeric_equivalent > number_to_convert quotient, remainder = number_to_convert.divmod(numeric_equivalent) number_to_convert = remainder roman_equivalent << roman_numeral * quotient end end ``` * I'd like to leave you with one last thought wrt solving this kata. [Jim Weirich when solving this kata at RubyConfIndia 2013](https://www.youtube.com/watch?v=ronr_CG8x0Y&nohtml5=False) said(start listening from `7:52` onwards), > I don't want to write code for special cases until I understand the general case first. For me, this was something I wasn't doing very consciously all this while but I felt, it's definitely something worth reflecting upon and further experimenting with. * Wrapping up, I would like to thank Jim for the above thought and for his work that has helped so many people. Also, my learnings here wouldn't have been possible without those who've contributed to exercism and to those whose solution I had an opportunity to refer to. I'm thankful to them as well.