Monkey patching an object
suggest changeLike patching of classes, you can also patch single objects. The difference is that only that one instance can use the new method.
Example: Override a string object to provide parsing to boolean
s = 'true'
t = 'false'
def s.to_b
self =~ /true/ ? true : false
end
# >> s.to_b
# => true
# >> t.to_b
# => undefined method 'to_b' for an instance of String (NoMethodError)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents