Inject problem

  • Thread starter Amir Ebrahimifard
  • Start date
A

Amir Ebrahimifard

Hi
In inject method , everytime that inject called what is returned ? for
example in this code everytime what is returned ?

array = [*1..10]
array.inject(0) { | memo , item | memo + item }
 
B

brabuhr

In inject method , everytime that inject called what is returned ? for
example in this code =A0everytime what is returned ?

array =3D [*1..10]
array.inject(0) { | memo , item | memo + item }

irb(main):001:0> array =3D [*1..10]
=3D> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
irb(main):002:0> array.inject(0) { | memo , item | puts
"#{memo}\t#{item}"; memo + item }
0 1
1 2
3 3
6 4
10 5
15 6
21 7
28 8
36 9
45 10
=3D> 55
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top