Hi -- On Sat, 26 Apr 2008, 7stud -- wrote: > Inbroker Adams wrote: >> Hello Rubyists, >> I am new to the language and the community. >> My first problem is this : >> >> i used the following code : >> >> sum = 0 >> [1,3,5].inject() {|sum, element| sum + element} >> print sum >> >> >> I expected to get 9 >> but instead i get 4 >> >> I just installed the Ruby 1.8.6 one-click installer on windows >> and wrote the above commands in SciTE. >> Any suggestions?? >> > > Don't ever use inject--forget you ever read about it. Use a hand > written loop of your own instead. Your code will be easier to > understand and it will be more efficient. inject isn't really hard to understand; it's just an iterator with a kind of feedback loop into the first block parameter. If you find it a bit tricky I'd still encourage you to study it and accustom yourself to it. It's awfully useful. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates!