Created
April 27, 2017 07:32
-
-
Save mactkg/858114e1529b012f7d24a95ca3d697d0 to your computer and use it in GitHub Desktop.
MixInの練習
This file contains hidden or 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
[1] pry(main)> module MixIn | |
[1] pry(main)* def hello | |
[1] pry(main)* puts "hello from MixIn" | |
[1] pry(main)* end | |
[1] pry(main)* end | |
=> :hello | |
[2] pry(main)> class A | |
[2] pry(main)* include MixIn | |
[2] pry(main)* end | |
=> A | |
[3] pry(main)> a = A.new | |
=> #<A:0x007faffa4bcda8> | |
[4] pry(main)> a.hello | |
hello from MixIn | |
=> nil | |
[5] pry(main)> class B | |
[5] pry(main)* include MixIn | |
[5] pry(main)* def hello | |
[5] pry(main)* puts "hello from class B" | |
[5] pry(main)* end | |
[5] pry(main)* end | |
=> :hello | |
[6] pry(main)> b = B.new | |
=> #<B:0x007faff99ffed8> | |
[7] pry(main)> b.hello | |
hello from class B | |
=> nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
継承ツリーの一番下からたどるのでMixIn先にmethodがあってもMixIn元が優先される