"Robert Klemme" <bob.news@gmx.net> writes:
There is no equivalent (yet) for "def foo(&b) b.call(1) end" with
define_method, because you cannot do define_method(:foo) {|&b|
b.call(1)}.
As Florian Frank pointed out, this is fixed in Ruby 1.9.
Also, you cannot have default values for method arguments
with [define_method].
I just wanted to point out that this is not a show-stopping problem.
In Ruby 1.9, `define_method' can do everything `def' can.
def foo(bar, baz = QUUX, *rest)
...
end
can be written as
define_method :foo do |bar, *rest|
baz = rest.empty? and QUUX or rest.shift
...
end
For longer parameter lists, you could refactor a bit:
class Array
def shift_or default
if empty? then default else shift end
end
end
define_method :foo do |bar, *rest|
baz1 = rest.shift_or QUUX1
baz2 = rest.shift_or QUUX2
baz3 = rest.shift_or QUUX3
...
end
But of course, you already know this.
As I said, I just wanted to point out that `def' and `define_method'
are essentially equal in power as of Ruby 1.9.
···
--
Daniel Brockman <daniel@brockman.se>
So really, we all have to ask ourselves:
Am I waiting for RMS to do this? --TTN.