Skip to content

Month: September 2015

Calculating a Percentage in Ruby Elegantly

In a recent project it would have been handy to write some ruby code which looked like: View the code on Gist. When searching through the ruby libraries there wasn’t any indication it existed. Wanting to avoid a class method – a quick google turned up this StackOverflow post – Calculate percentage in ruby. It presents a nice solution to the problem (one I should have thought of!). Create a class called Numeric and add the method to it: View the code on Gist. Now a number object has a method called percent_of which takes itself and another number as an argument to…