Created
September 27, 2011 15:00
-
-
Save benolee/1245283 to your computer and use it in GitHub Desktop.
Playing around with inheritance vs. inclusion
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
require 'minitest/spec' | |
require 'minitest/autorun' | |
describe "class inheritance" do | |
it "inherits public singleton methods" do | |
superclass = Class.new | |
superclass.define_singleton_method(:pub_sing_meth) {} | |
subclass = Class.new(superclass) | |
subclass.singleton_methods.include?(:pub_sing_meth).must_equal true | |
end | |
it "does not inherit private singleton methods" do | |
superclass = Class.new | |
superclass.define_singleton_method(:priv_sing_meth) {} | |
superclass.singleton_class.__send__(:private, :priv_sing_meth) | |
subclass = Class.new(superclass) | |
subclass.singleton_methods.include?(:priv_sing_meth).must_equal false | |
end | |
end | |
describe "module inclusion" do | |
it "defines instance methods from the inherited module" do | |
test_module = Module.new { define_method(:imeth) {} } | |
test_class = Class.new { include test_module } | |
test_class.instance_methods.include?(:imeth).must_equal true | |
end | |
it "does not define the module's public singleton methods" do | |
test_module = Module.new { define_singleton_method(:pub_sing_meth) {} } | |
test_class = Class.new { include test_module } | |
test_class.singleton_methods.include?(:pub_sing_meth).must_equal false | |
end | |
end |
Author
benolee
commented
Sep 27, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment