The reason for this is that define_method defines the method somewhat differently than def. This is due to the creation of anonymous procs and lambdas. I would suggest just using the method name, since you already have it. This should not distort the stack trace for the method name, so it should work better:
class Testing [:one, :two].each do |name| define_method name do "This method name is #{name}." end end end Testing.new.one => This method name is one. Testing.new.two => This method name is two.
To clarify, pay attention to what is returned by the following two statements:
class Testing define_method :one do __method__ end end => #<Proc: 0x000001009ebfc8@ (irb):54 (lambda)> class Testing def one __method__ end end => nil
PS: There is also a performance difference between the two formats. You can verify that def is faster than define_method using Benchmark.
source share