-
-
Save matthewrudy/218649 to your computer and use it in GitHub Desktop.
This file contains 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
# I'm creating some classes and modules on the fly, but things aren't hooking up quite the way I'd hoped. | |
# Can anyone explain why when an instance of Mod::Sibling tries to call Child.cry, that it | |
# doesn't call Mod::Child.cry, but instead looks for Behaviour::Child? | |
class Base | |
def self.cry | |
"#{self.name}.cry called!" | |
end | |
end | |
module Behaviour | |
def blame | |
puts "I am #{self}, a #{self.class.name}, trying to call Child.cry" | |
Child.cry | |
end | |
end | |
module Mod | |
class Child < Base | |
end | |
end | |
Mod::Child.cry # => "Mod::Child.cry called!" | |
Mod::Child.name # => "Mod::Child" | |
module Mod | |
class Sibling < Base | |
include Behaviour | |
end | |
end | |
Mod::Sibling.name # => "Mod::Sibling" | |
Mod.constants # => ["Child", "Sibling"] | |
sibling = Mod::Sibling.new | |
sibling.blame # => | |
# ~> -:10:in `blame': uninitialized constant Behaviour::Child (NameError) | |
# ~> from -:32 | |
# >> I am #<Mod::Sibling:0x1973078>, a Mod::Sibling, trying to call Child.cry |
This file contains 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
# I'm creating some classes and modules on the fly, but things aren't hooking up quite the way I'd hoped. | |
# Can anyone explain why when an instance of Mod::Sibling tries to call Child.cry, that it | |
# doesn't call Mod::Child.cry, but instead looks for Behaviour::Child? | |
class Base | |
def self.cry | |
"#{self.name}.cry called!" | |
end | |
end | |
module Behaviour | |
def blame | |
puts "I am #{self}, a #{self.class.name}, trying to call Child.cry" | |
Child.cry | |
end | |
end | |
child = Class.new(Base) | |
Mod = Module.new | |
Mod.const_set(:Child, child) | |
child.cry # => "Mod::Child.cry called!" | |
Mod::Child.cry # => "Mod::Child.cry called!" | |
child.name # => "Mod::Child" | |
sibling = Class.new(Base) | |
sibling.class_eval do | |
include Behaviour | |
end | |
Mod.const_set(:Sibling, sibling) | |
sibling.name # => "Mod::Sibling" | |
Mod.constants # => ["Child", "Sibling"] | |
sibling = Mod::Sibling.new | |
sibling.blame # => | |
# ~> -:10:in `blame': uninitialized constant Behaviour::Child (NameError) | |
# ~> from -:32 | |
# >> I am #<Mod::Sibling:0x1973078>, a Mod::Sibling, trying to call Child.cry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment