what is the uses of inject()

V

Vellingiri Arul

Hello friends,
Can anybody tell me the answer.

what is the uses of inject().
Give some example.

by
Vellingiri.
 
S

Sebastian Hungerecker

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).

Give some example.

[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
 
V

Vellingiri Arul

anders said:
You have a lot of Q and what examples have a look in
http://www.ruby-doc.org/docs/UsersGuide/rg/
//AP

very very thank you friends.

You have asked one question know,that is not a example program question.
I am also working one software company,I have faced lot of doubts in my
ruby program.
Some thing general what we were discussed the questions.

Ok.

by

vellingiri.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,323
Members
48,007
Latest member
Elvis60357

Latest Threads

Top