Add Elements In an Array


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your 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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.