Monkey patching an object

suggest change

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
end

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

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents