Skip to content

Instantly share code, notes, and snippets.

@mactkg
Created April 27, 2017 07:32
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 mactkg/858114e1529b012f7d24a95ca3d697d0 to your computer and use it in GitHub Desktop.
Save mactkg/858114e1529b012f7d24a95ca3d697d0 to your computer and use it in GitHub Desktop.
MixInの練習
[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
@mactkg
Copy link
Author

mactkg commented Apr 27, 2017

継承ツリーの一番下からたどるのでMixIn先にmethodがあってもMixIn元が優先される

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment