From: "Eustáquio 'TaQ' Rangel" Date: 2008-03-27T04:04:30+09:00 Subject: Re: The inject function -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Robert Klemme wrote: | On 26.03.2008 15:32, Eust�quio 'TaQ' Rangel wrote: |> You could use a test to sum the number only when it's even on inject, but |> [1,2,3,4,5,6].inject {|m,v| m + (v%2==0?v:0) } |> results to 13, because seems that the value of m is automatically the |> first |> element of the array. On this case, you can tell it to start with 0: |> [1,2,3,4,5,6].inject(0) {|m,v| m + (v%2==0?v:0) } | Actually these two are not equivalent! You should usually use the | second form because the first form will return nil for an empty Array. Hi Robert! Yeah, and I just put the first form because at first glance they look equivalent for newbies on the language and can make some confusion for "where are the 1 value coming from". :-) Regards, -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQFH6p+pb6UiZnhJiLsRAhwnAJ96znoMtgWQSmGg/R7fNzkNbo0tmACgnuXQ EB5r2goC0lh3WSxTsoLBN64= =vvYN -----END PGP SIGNATURE-----