Posted By

lakedenman on 05/12/08


Tagged

array ruby inject


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Add Elements In an Array


 / Published in: Ruby
 

URL: http://blog.jayfields.com/2008/03/ruby-inject.html

  1. #Set up the hours array
  2. hours = [8, 8, 8, 8]
  3.  
  4. #Run the inject method on the Array
  5. total_hours = hours.inject {|result, element| result + element}
  6.  
  7. #Output total hours
  8. puts total_hours #=> 32

Report this snippet  

You need to login to post a comment.