Dan Diebolt
3/26/2008 2:43:00 PM
[Note: parts of this message were removed to make it a legal post.]
> I want to add together numbers in an array with the inject command. I
want only the even numbers using only inject.
a=[1,2,3,4]
a.inject(0) {|x,y| y % 2 == 0 ? x+y : x}
=> 6
>Second, I need to reverse an array using only the inject command. So,
something like above with the result [4,3,2,1]
a=[1,2,3,4]
a.inject([]) {|x,y| x.unshift(y)}
=> [4, 3, 2, 1]
It seems like an artificial requirement to use only "inject".