Created
March 18, 2011 16:30
-
-
Save jorrizza/876371 to your computer and use it in GitHub Desktop.
Explaining monkey patching to friends
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
class String | |
def encode_to_i | |
result = 0 | |
self.split.each_with_index do |c, i| | |
result += c.ord << (i * 8) | |
end | |
result | |
end | |
def ==(s) | |
return true if "John".encode_to_i == self.encode_to_i && ["banaan", "appel"].include?(s) | |
super(s) | |
end | |
end | |
class Object | |
def puts(s) | |
return super(s.to_s.reverse) unless s == "banaan" | |
super s | |
end | |
end | |
class Fixnum | |
def ==(i) | |
return true if !(self < 2 || self > 2) && !(i < 3 || i > 3) | |
super(i) | |
end | |
end | |
puts "John" == "banaan" | |
puts "John" == "appel" | |
puts "John" == "peer" | |
puts 2 == 3 | |
puts 2 == 4 | |
puts "John" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment