Ruby Language Monkey patching an object


Like 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

>> s.to_b
=> true
>> t.to_b
=> undefined method `to_b' for "false":String (NoMethodError)