class Fixnum
# convert minutes to hours in decimal format (75 minutes is 1.25 hours)
def to_hours
(self.to_f / 60).round(2)
end
end
class MySexyClass < SuperClass
def to_hours
(self.to_f / 60).round(2)
end
end
MySexyClass.new(125).to_hours
If you, say, forgot to require 'integer' before running this monkey patch, you’ll accidentally redefine Integer instead of patching it.
lib/core_extensions/integer/hours.rb
module CoreExtensions
module Integer
module Hours
def to_hours
(self.to_f / 60).round(2)
end
end
end
end
config/initializers/extensions.rb
# Actually monkey-patch Integer
Integer.include CoreExtensions::Integer::Hours