Skip to content

Instantly share code, notes, and snippets.

@nekonenene
Last active July 21, 2017 02:42
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 nekonenene/314634d8fdb5ee386378067f430ed984 to your computer and use it in GitHub Desktop.
Save nekonenene/314634d8fdb5ee386378067f430ed984 to your computer and use it in GitHub Desktop.
refine / using で安全にオブジェクトへメソッド追加
module StringExtend
refine String do
def reverse
"aaa"
end
end
end
module TestModule
def output_from_module
puts "my_string".reverse
end
end
class TestClass
using StringExtend
include TestModule
def output_from_class
puts "my_string".reverse
end
end
test = TestClass.new
test.output_from_module # Output: gnirts_ym
test.output_from_class # Output: aaa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment