演算子を引数として取る関数の定義の続き(読みにくいかも)
もっと楽ができるかもしれない。
class Hoge < Array def f(op,x) if block_given? inject(x) { |n,i| n.send(op, yield(i))} else inject(x) { |n,i| n.send(op, i)} end end end hoge=Hoge.new.concat((1..10).to_a) p hoge.f(:+,0) #=>55 p hoge.f(:+,0) {|e| e**2} #=>385 #(1..10).inject(0){|sum,i| sum + i**2} と同じ。 #(1+4+9+25+...100)=385
「f(op1, op2, x, num)」とかにならないのが見やすくていい