Created
October 23, 2014 10:39
-
-
Save saxxi/a0a04b14534970662beb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Instead of monkeypatching globally methods | |
# it is possible to `refine` methods | |
# so you know where a method is coming from. | |
# | |
# Ref: | |
# - http://rkh.im/ruby-2.1 | |
# - http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html | |
# | |
# Note: | |
# - it was intruced in ruby 2.0 and became stable in ruby 2.1. | |
module FunnyNumbers | |
refine Fixnum do | |
def double_me | |
self * 2 | |
end | |
end | |
end | |
class Location | |
using FunnyNumbers | |
def calculate | |
20.double_me + 1 | |
end | |
end | |
puts Location.new.calculate |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment