From: Rob Biedenharn Date: 2009-11-26T01:42:41+09:00 Subject: Re: Ruby internals & other questions On Nov 25, 2009, at 11:29 AM, Aldric Giacomoni wrote: > Ralph Shnelvar wrote: >> >> y=0 >> 1_000_000.times {|x| y+=x} > > y = (1..1_000_000).inject { |a, b| a + b } > > More idiomatic, though maybe yours is easier to read at first. > If you're not sure what it does, read the rdoc, then ask us questions > (rdocs aren't -always- crystal clear). > To compare which one is faster, a google search with the keywords : > "ruby, benchmark, bmbm" will help. > ...but not equivalent: y=0 1_000_000.times {|x| y+=x } y = (0..999_999).inject {|a,b| a+b} Although I wouldn't necessarily call this use of inject idiomatic. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com