Hi –
On Sun, 24 Dec 2006, Michal K. wrote:
end
class Module
def override! method
With this code, to override the system method you can write (syntax
inspired by RSpec):
Kernel.override!(:system).and_return false
and then to restore:
Kernel.restore! :system
Can you suggest any improvements to this code?
A couple of things, but be warned I am still early in my coffee today
You don’t need to_sym in those calls to alias_method; it will take a
string. In fact, you can do this:
“orig_#{method}”
which will work for either.
The ! is generally used for methods that come in pairs: override and
override! It indicates that one is the “dangerous” version of the
operation. In this case, there’s only one of each, and the name
itself describes what’s happening, so I’d lose the !'s. (I’ll keep
them in my examples below, though.)
I would avoid “and_return”, which is a kind of odd method name (it
leaves me thinking: don’t methods always return something?). It’s
better just to tell the method what you want, and not string it across
multiple method calls. Just give it two arguments, or a hash of
arguments.
Here’s another version, for your reading pleasure. Use only as
desired.
class Module
def override(opts)
method, value = opts.values_at(:method, :value)
alias_method “orig_#{method}”, method
define_method(method) { value }
end
def restore(method)
alias_method method,“orig_#{method}”
end
end
Kernel.override(:method => “system”, :value => 3)
puts system
Kernel.restore(:system)
system(“date”)
What would be more general would be a way to specify the new behavior
arbitrarily, with a code block, which the import-module library does.
David