eprothro
12/8/2015 - 6:37 PM

method chain order.md

irb(main):001:0> module A
irb(main):002:1>   def sound_off
irb(main):003:2>     puts 'module a'
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0>
irb(main):007:0* module B
irb(main):008:1>   def sound_off
irb(main):009:2>     puts 'module b'
irb(main):010:2>   end
irb(main):011:1> end
=> nil
irb(main):018:0>
irb(main):019:0* class Parent
irb(main):020:1>   def sound_off
irb(main):021:2>     puts 'parent'
irb(main):022:2>   end
irb(main):023:1> end
=> nil
irb(main):024:0>
irb(main):025:0* class Override < Parent
irb(main):026:1>   include A
irb(main):027:1>   include B
irb(main):028:1>
irb(main):029:1*   def sound_off
irb(main):030:2>     puts 'c'
irb(main):031:2>   end
irb(main):032:1> end
=> nil
irb(main):033:0>
irb(main):034:0* Override.new.sound_off
c
=> nil
irb(main):035:0>
irb(main):036:0* class ModuleBeforeParent
irb(main):037:1>   include A
irb(main):038:1>   include B
irb(main):039:1>
irb(main):040:1*   def sound_off
irb(main):041:2>     super
irb(main):042:2>   end
irb(main):043:1> end
=> nil
irb(main):044:0>
irb(main):045:0* ModuleBeforeParent.new.sound_off
module b
=> nil
irb(main):046:0>
irb(main):047:0* module PassUpTheChain
irb(main):048:1>   def sound_off
irb(main):049:2>     super
irb(main):050:2>   end
irb(main):051:1> end
=> nil
irb(main):052:0>
irb(main):053:0* class ModuleAccessesParent < Parent
irb(main):054:1>   include PassUpTheChain
irb(main):055:1>
irb(main):056:1*   def sound_off
irb(main):057:2>     super
irb(main):058:2>   end
irb(main):059:1> end
=> nil
irb(main):060:0>
irb(main):061:0* ModuleAccessesParent.new.sound_off
parent
=> nil
irb(main):062:0>
irb(main):063:0* class ModuleOrderWins
irb(main):064:1>   include B
irb(main):065:1>   include A
irb(main):066:1>
irb(main):067:1*   def sound_off
irb(main):068:2>     super
irb(main):069:2>   end
irb(main):070:1> end
=> nil
irb(main):071:0>
irb(main):072:0* ModuleOrderWins.new.sound_off
module a
=> nil
irb(main):001:0> require 'active_support'
=> true
irb(main):002:0> class ParentClass
irb(main):003:1>
irb(main):004:1*   def self.mix
irb(main):005:2>     include RuntimeMixin
irb(main):006:2>   end
irb(main):007:1>
irb(main):008:1*   def sound_off
irb(main):009:2>     "Parent"
irb(main):010:2>   end
irb(main):011:1> end
=> :sound_off
irb(main):012:0>
irb(main):013:0* module RuntimeMixin
irb(main):014:1>   extend ActiveSupport::Concern
irb(main):015:1>
irb(main):016:1*   def sound_off
irb(main):017:2>     "Mixin Applied at run time"
irb(main):018:2>   end
irb(main):019:1> end
=> :sound_off
irb(main):020:0>
irb(main):021:0* class Child < ParentClass
irb(main):022:1> end
=> nil
irb(main):023:0>
irb(main):024:0* Child.mix
=> Child
irb(main):025:0> c  = Child.new
=> #<Child:0x007f9d54051ec0>
irb(main):026:0> c.sound_off
=> "Mixin Applied at run time"
irb(main):027:0>
irb(main):028:0* class ChildWithOverride < ParentClass
irb(main):029:1>   def sound_off
irb(main):030:2>     "child override"
irb(main):031:2>   end
irb(main):032:1> end
=> :sound_off
irb(main):033:0>
irb(main):034:0* ChildWithOverride.mix
=> ChildWithOverride
irb(main):035:0> c  = ChildWithOverride.new
=> #<ChildWithOverride:0x007f9d53052358>
irb(main):036:0> c.sound_off
=> "child override"
irb(main):037:0>
irb(main):038:0* class ChildWithDeferringOverride < ParentClass
irb(main):039:1>   def sound_off
irb(main):040:2>     "child with deferring override portion"
irb(main):041:2>     super
irb(main):042:2>   end
irb(main):043:1> end
=> :sound_off
irb(main):044:0>
irb(main):045:0* ChildWithDeferringOverride.mix
=> ChildWithDeferringOverride
irb(main):046:0> c  = ChildWithDeferringOverride.new
=> #<ChildWithDeferringOverride:0x007f9d550070e0>
irb(main):047:0> c.sound_off
=> "Mixin Applied at run time"
irb(main):048:0>