Created
August 7, 2012 06:44
-
-
Save remino/3282434 to your computer and use it in GitHub Desktop.
Fooling around with Ruby includes and extends in classes and modules
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
#!/usr/bin/ruby | |
HashA = {a: 1, b: 2} | |
HashB = {b: 3, c: 4} | |
HashC = {c: 5, d: 6} | |
module Modules | |
module ModuleA | |
def self.included base | |
base.class_eval do | |
extend ClassMethods | |
end | |
base.class.class_eval do | |
attr_accessor :foo_hash | |
end | |
end | |
def new_instance_method | |
true | |
end | |
def foo_hash | |
self.class.foo_hash | |
end | |
module ClassMethods | |
def new_class_method | |
true | |
end | |
def set_hash hash | |
self.foo_hash = hash | |
end | |
end | |
end | |
end | |
module Classes | |
class ClassA | |
include Modules::ModuleA | |
set_hash HashA | |
end | |
class ClassB < ClassA | |
set_hash HashB | |
end | |
class ClassB::ClassC | |
end | |
end | |
instance_a = Classes::ClassA.new | |
instance_b = Classes::ClassB.new | |
instance_c = Classes::ClassB::ClassC.new | |
instance_d = Class.new Classes::ClassB | |
puts "instance_b.class:", instance_b.class | |
puts "instance_d.class:", instance_d.class | |
puts "instance_a.foo_hash:", instance_a.foo_hash | |
puts "instance_b.foo_hash:", instance_b.foo_hash | |
instance_e = Classes::ClassB.dup.new | |
puts "instance_e.class (before const_set):", instance_e.class | |
Classes::const_set 'ClassF', instance_e.class | |
Classes::ClassF.set_hash HashC | |
instance_f = Classes::ClassF.new | |
puts "instance_e.class (after const_set):", instance_e.class | |
puts "instance_f.class:", instance_e.class | |
puts "instance_b.foo_hash (after ClassB.dup):", instance_b.foo_hash | |
puts "instance_f.foo_hash:", instance_f.foo_hash | |
puts "Classes::ClassB.respond_to?(:new_class_method) == true", | |
Classes::ClassB.respond_to?(:new_class_method) == true | |
puts "Classes::ClassB.respond_to?(:new_instance_method) == false", | |
Classes::ClassB.respond_to?(:new_instance_method) == false | |
puts "instance_b.respond_to?(:new_class_method) == false", | |
instance_b.respond_to?(:new_class_method) == false | |
puts "instance_b.respond_to?(:new_instance_method) == true", | |
instance_b.respond_to?(:new_instance_method) == true | |
puts "instance_d.respond_to?(:new_class_method) == false", | |
instance_d.respond_to?(:new_class_method) == false | |
puts "instance_d.respond_to?(:new_instance_method) == true", | |
instance_d.respond_to?(:new_instance_method) == true | |
puts "instance_e.respond_to?(:new_class_method) == false", | |
instance_e.respond_to?(:new_class_method) == false | |
puts "instance_e.respond_to?(:new_instance_method) == true", | |
instance_e.respond_to?(:new_instance_method) == true | |
puts "instance_f.respond_to?(:new_class_method) == false", | |
instance_f.respond_to?(:new_class_method) == false | |
puts "instance_f.respond_to?(:new_instance_method) == true", | |
instance_f.respond_to?(:new_instance_method) == true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment