Created
January 21, 2011 00:31
-
-
Save samstokes/789022 to your computer and use it in GitHub Desktop.
Specs for when module inclusion does and doesn't cause method overriding
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
module Existing | |
def twinge | |
"Existing#twinge" | |
end | |
def prunge | |
"Existing#prunge" | |
end | |
end | |
# module included in another module | |
# does not override existing methods | |
# includes methods that do not shadow existing ones | |
module EnhancementIncludedInModule | |
def twinge | |
"EnhancementIncludedInModule#twinge" | |
end | |
def ponge | |
"EnhancementIncludedInModule#ponge" | |
end | |
end | |
module Existing | |
include EnhancementIncludedInModule | |
end | |
# module included afterward in the same class | |
# overrides existing methods | |
# includes methods that do not shadow existing ones | |
module EnhancementIncludedInClass | |
def prunge | |
"EnhancementIncludedInClass#prunge" | |
end | |
def munge | |
"EnhancementIncludedInClass#munge" | |
end | |
end | |
class Klass | |
include Existing | |
include EnhancementIncludedInClass | |
end | |
describe Klass do | |
describe 'module included in another module' do | |
it 'does not override existing methods' do | |
subject.twinge.should == "Existing#twinge" | |
end | |
it 'includes methods that do not shadow existing ones' do | |
subject.ponge.should == "EnhancementIncludedInModule#ponge" | |
end | |
end | |
describe 'module included afterward in the same class' do | |
it 'overrides existing methods' do | |
subject.prunge.should == "EnhancementIncludedInClass#prunge" | |
end | |
it 'includes methods that do not shadow existing ones' do | |
subject.munge.should == "EnhancementIncludedInClass#munge" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment