Vellingiri said:
Hello friends,
Can anybody tell me the answer.
what is the uses of inject().
ri Enumerable#inject can give you a more elaborate answer, but here's mine:
It allows you to combine the elements of an array using a block (passing the
initial value or the result of the last iteration as well as the current item
to the block each iteration).
[1,2,3,4].inject {|s,i| s+i}
#=> 10 (1+2+3+4)
some_string="haolaolai foo tadam tadum bar HuHu chunky lamb bacon"
regexen=[/la./,/foo.*bar/,/huhu/i]
regexen.inject(some_string) {|str,re| str.gsub(re,"")}
#=> "hao chunky b bacon"
#(same as some_string.gsub(/la./,"").gsub(/foo.*bar/,"").gsub(/huhu/i,"") )
HTH,
Sebastian