Posted By

lakedenman on 05/12/08


Tagged

array ruby inject


Versions (?)



Who likes this?

1 person has marked this snippet as a favorite

webstic


Add Elements In an Array


Published in: Ruby 



Website Promotion
DIRECTORY
is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


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

Expand | Embed | Plain Text
  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.