:* - . - . :* "*", , .
ruby . .*(second) . , 3.*(4) 3*4. 3*4 .
ruby public_send . 3.public_send(:*, 4) , 3*4.
public_senT, .
[ 1, 2, 3, 4 ].inject(:*)
'*' , inject :
[ 1, 2, 3, 4 ].inject(:*) == 1 * 2 * 3 * 4
, 1 * 2 * 3 * 4 : * , , .
module Enumerable
def inject_asterisk
tally = first
rest = slice(1, length - 1)
rest.each do |next_num|
tally = tally * next_num
end
return tally
end
end
[2, 3, 5].inject_asterisk
, , tally next_number, tally . ruby , .
module Enumerable
def inject_block(&block)
tally = first
rest = slice(1, length - 1)
rest.each do |next_num|
tally = block.call(tally, next_num)
end
return tally
end
end
[2, 3, 5].inject_block {|tally, next_num| tally + next_num }
{|tally, next_num| tally.method_of_tally(next_num) }
( tally + next_num < == > tally.+(next_num) < == > tally.public_send(:+,next_num), :method_of_tally .
module Enumerable
def my_inject(method_of_tally_symbol, &block)
if method_of_tally_symbol
block = Proc.new { |tally, next_num|
tally.public_send(method_of_tally_symbol, next_num)
}
end
tally = first
rest = slice(1, length - 1)
rest.each do |next_num|
tally = block.call(tally, next_num)
end
return tally
end
end
[2, 3, 5].my_inject(:+)
, .