Sum & Product of an Array Icon

Sum & Product of an Array

How to sum all the numbers in an array,

Sum & Product of an ArrayOverview

Editor: How to sum all the numbers in an array, or get the multiplication product of the same numbers, without resorting to an ugly for loop? Ruby's inject to the rescue; this Snippet extends the array class with the methods sum and product, which can hereafter be used on any array in Ruby. Note that the parameter to inject is the initial value of s, the result, and thus obviously different depending on whether we're doing addition or multiplication.

NEW

Fixed some bugs.

Sum & Product of an ArrayInformation

Version
N/A
Date
08.20.10
License
Language
English
File Size
N/A
Developer
Category
SubCategory
Operating Systems
Windows ,Linux,Mac OS
System Requirements
No additional system requirements.
Ordinals for All Numeric Types Icon
This snippet will extend all numerical types with a method for returning .
Free
Generate Random Password ruby Icon
Generates a secure, entirely random password of the specified length.
More
MP4Info Icon
MP4Info is a library providing read (and, someday, write) access to MP4 tags.
Free
GoogleService Icon
GoogleService is a library to use Google data API easily.
WEBrick servlet skeleton Icon
Skeleton code for a WEBrick servlet.
Free
Using gsub with blocks to strip attributes Icon
This is a script for ruby.
Free
Net::DNS Icon
Net::DNS is a pure-Ruby DNS library,
Free
Weft QDA Icon
Weft QDA is an easy-to-use GUI package for the analysis
Demo
Net-SIP Icon
Net-SIP will implements all SIP protocol operation using EventMachine network.
Free
RImage Icon
RImage is an image library with focus on loading and saving images.
More