Skip to content

Instantly share code, notes, and snippets.

@aeden
Created July 7, 2011 08:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aeden/1069124 to your computer and use it in GitHub Desktop.
Save aeden/1069124 to your computer and use it in GitHub Desktop.
Temporarily Overriding A Method in Ruby
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