Metaprogramming with String Evaluations : define_method « Method « Ruby






Metaprogramming with String Evaluations


class Numeric
 [['add', '+'], ['subtract', '-'],
  ['multiply', '*',], ['divide', '/']].each do |method, operator|
    define_method("#{method}_2") do
      method(operator).call(2)
    end
  end
end

 








Related examples in the same category

1.define_method method allows you to create methods on the fly.
2.Add a new method with define_method