Skip to content

Instantly share code, notes, and snippets.

@saxxi
Created October 23, 2014 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saxxi/a0a04b14534970662beb to your computer and use it in GitHub Desktop.
Save saxxi/a0a04b14534970662beb to your computer and use it in GitHub Desktop.
# 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