I could do some simple task in either way,
Refinements
module StringRefinements
refine String do
def do_something
"bla bla bla..."
end
end
end
So, I can use do_something method wherever StringRefinements module was using.
Monkeypatch
class String
def do_something
"bla bla bla..."
end
end
I would like to know the difference between Ruby's new concept Refinements and the one Monkeypatch. And what are the advantages of using Refinements over Monkeypatch?
The most significant different between refinements and monkey-patching is that monkey-patching changes every single instance in your application. This might not be a problem when you are adding a method that doesn't otherwise exist, but when you are redefining an existing method, it can very easily break expectations elsewhere in the code.
When you use refinements, the behaviour is only changed where you use the
usingmethod to activate the refinements. This is safer because your changed methods cannot "leak" into the rest of your code (or dependencies).