Skip to content

Instantly share code, notes, and snippets.

@samstokes
Created January 21, 2011 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samstokes/789022 to your computer and use it in GitHub Desktop.
Save samstokes/789022 to your computer and use it in GitHub Desktop.
Specs for when module inclusion does and doesn't cause method overriding
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