Mega Code Archive

 
Categories / Ruby / Method
 

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