Is a monkey patch when you extend a class?
class Hash
def delete_blanks!
delete_if { |k, v| v.is_nil? }
end
end
Then you can do this:
h = { red: 'stop', yellow: 'ready', purple: nil, green: 'go'}
h.delete_blanks! #=> { red: 'stop', yellow: 'ready', green: 'go' }
Is that a monkey patch? What about this:
class ActiveRecord::Base
def foo
"bar"
end
end
What's so bad about that?
I'm not being argumentative, I'm ready to assume it is bad, but how should I go about emulating this behaviour without a monkey patch? Should I send
the method?
I wouldn't say monkey patching is bad, it's more like a "should avoid" practice, there are definitely scenarios where monkey patching is useful.
Another way to solve this problem is through inheritance so you could have something like:
class SuperHash < Hash
def delete_blanks!
delete_if { |k, v| v.is_nil? }
end
end