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.
Lots of Array methods Icon
This is a script for ruby.
Free
Isefad roguelike game Icon
This is an extensible roguelike game, written in Ruby.
Making if and unless consistent? Icon
I've been thinking why some Ruby constructions are inconsistent.
Free
PNG-24 Alpha support for IE Icon
Get alpha channel on web, that's possible with PNG-24 images and this trick.
Free
Real Target Icon
Find the real target of a symbolic link.
Free
Calculating ezmlm-hash of an email address Icon
Calculates the ezmlm-hash of an email address for use with mysql.
Free
More