Reputation: 8305
While looking through ActiveSupport source code I've noticed that sometimes eval
is used in places where define_method
is enough.
Example: ActiveSupport: Module.delegate
I consider define_method
more clean and safe way of doing things.
What is the benefits of eval
over define_method
?
Perfomance, memory usage, something else?
Upvotes: 7
Views: 1662
Reputation: 12225
I don't know what the reason in that particular case, but define_method
takes a block, which is a closure (carries local variables of the place it was defined), and that can lead to considerably higher memory consumption comparing to plain eval
.
Upvotes: 2
Reputation: 39620
I found this to be a very nice article on the subject: http://blog.grayproductions.net/articles/eval_isnt_quite_pure_evil.
Upvotes: 3
Reputation: 81510
When you use define_method, the method you're defining can't accept a block.
It’s pretty well known that because of a deficiency in blocks arguments in Ruby 1.8 Class#define_method cannot define methods that take blocks.
def x *args, █ end # => works!
define_method(:x) {|*args,&block| } # => SyntaxError: compile error
The method being defined requires a block:
"def #{prefix}#{method}(*args, &block)" # def customer_name(*args, &block)
So define_method can't be used.
Upvotes: 3