Skip to content

Instantly share code, notes, and snippets.

@benolee
Created September 27, 2011 15:00
Show Gist options
  • Save benolee/1245283 to your computer and use it in GitHub Desktop.
Save benolee/1245283 to your computer and use it in GitHub Desktop.
Playing around with inheritance vs. inclusion
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
@benolee
Copy link
Author

benolee commented Sep 27, 2011

$ ruby inheritance_test.rb 
Loaded suite inheritance_test
Started
....
Finished in 0.000451 seconds.

4 tests, 4 assertions, 0 failures, 0 errors, 0 skips

Test run options: --seed 16536

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