Skip to content

Instantly share code, notes, and snippets.

@jorrizza
Created March 18, 2011 16:30
Show Gist options
  • Save jorrizza/876371 to your computer and use it in GitHub Desktop.
Save jorrizza/876371 to your computer and use it in GitHub Desktop.
Explaining monkey patching to friends
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