Skip to content

Instantly share code, notes, and snippets.

@MacoTasu MacoTasu/module_prepend.rb

Last active Jul 13, 2016
Embed
What would you like to do?
Module#prepend実行結果
[1] pry(main)> module Hoge
[1] pry(main)* def say
[1] pry(main)* p "hoge"
[1] pry(main)* end
[1] pry(main)* end
=> :say
[2] pry(main)> module Fuga
[2] pry(main)* def say
[2] pry(main)* p "fuga"
[2] pry(main)* end
[2] pry(main)* end
=> :say
[3] pry(main)> class Test
[3] pry(main)* def initialize(name="")
[3] pry(main)* self.class.prepend Object.const_get "#{name}"
[3] pry(main)* end
[3] pry(main)* end
=> :initialize
[4] pry(main)> Test.new("Hoge").say
"hoge"
=> "hoge"
[5] pry(main)> Test.new("Fuga").say
"fuga"
=> "fuga"
[6] pry(main)> Test.new("Hoge").say
"fuga"
=> "fuga"
[7] pry(main)>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.