そこで__send__ですよ
1.8.6 で 1.8.7 な inject を使う - gan2 の Ruby 勉強日記
injectは引数を省略できるし、四則演算以外のシンボルにも対応している。よーく見てみれば__send__を使えばいいことに気付くだろう。
module Enumerable alias_method :_inject, :inject def inject(arg=nil, &block) if arg.class == Symbol _inject {|s,x| s.__send__(arg, x)} elsif arg _inject(arg, &block) else _inject(&block) end end end (1..4).inject(:+) # => 10 (1..4).inject(:-) # => -8 [50, 2, 5].inject(:*) # => 500 [50, 2, 5].inject(:/) # => 5 (1..4).inject{|s,x| s+x} # => 10 (1..4).inject(0){|s,x| s+x} # => 10
ちなみに実行結果の注釈はxmpfilterを使うと楽。