Created
July 7, 2011 08:53
-
-
Save aeden/1069124 to your computer and use it in GitHub Desktop.
Temporarily Overriding A Method in Ruby
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
module Count | |
def fake_count | |
words.count + 500 | |
end | |
end | |
class Thing | |
def words | |
%w(one two three) | |
end | |
def count | |
words.count | |
end | |
end | |
def assert(value) | |
raise "Assertion failed" unless value == true | |
end | |
thing = Thing.new | |
assert thing.count == 3 | |
puts thing.count | |
class Thing | |
include Count | |
alias_method :real_count, :count | |
alias_method :count, :fake_count | |
end | |
thing = Thing.new | |
assert thing.count == 503 | |
puts thing.count | |
class Thing | |
alias_method :count, :real_count | |
end | |
thing = Thing.new | |
assert thing.count == 3 | |
puts thing.count |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment