Is there an easier and/or more readable way to create a closure in Ruby so that the defined method has access to the variable m?
I have a slight “problem” with the lambda here.
Very often I dynamically define methods that have to access a local variable:
For example:
class Comparison
def income
123
end
def sales
42342
end
# and a dozen of other methods
# Generate xxx_after_tax for each method
instance_methods(false).each do |m|
lambda {
define_method("#{m}_after_tax") do
send(m) * 0.9
end
}.call
end
end
1 Answer