Reputation: 22636
Hi I'm trying use a mixin to define some method using define_method. I would like to do something like
module A
%w(a b c d).each do |w|
define_method(w) { "method #{w}" }
end
end
So I can then do
class B
include A
end
B.new.c # => 'method c'
But that doesn't work.
I've tried (almost) everything involving included, instance_eval class_eval etc... nothign work.
Is it possible to do ?
There were initially a syntax error in the example I posted, but that wasn't the real problem. I just made the example for the post, that wasn't the real example (which a bit too long to be posted).
My problem was I used a comma in my list %w(a, b, c, b)
instead of %w(a b c b)
Therefore, a,
was defined instead of a
. Silly me, (hard to spot though).
Seeing that it works for you guys helped me solving the problem, thanks
Upvotes: 0
Views: 253
Reputation: 301587
Shouldn't the module be:
module A
%w(a b c d).each do |w|
define_method(w) { "method #{w}" }
end
end
that is, the end
was missing.
Upvotes: 4