L
Louis J Scoras
This works fine
The assignment to sum is useless. Only the return value matters to inject.
arr.inject(0) { |sum, i| sum + ( i == 3 ? 0 : i ) }
Does the same thing. Actually, its more correct. The above is only
doing what you want because an assignment returns the value it assigns
to:
sum += i
sum = sum + i
sum + i #evaluates to this after side-effect
sum = 0
arr = (1..10).to_a
arr.inject(0) { |sum, i| sum += ( i == 3 ? 0 : i ) } => 52
The assignment to sum is useless. Only the return value matters to inject.
arr.inject(0) { |sum, i| sum + ( i == 3 ? 0 : i ) }
Does the same thing. Actually, its more correct. The above is only
doing what you want because an assignment returns the value it assigns
to:
sum += i
sum = sum + i
sum + i #evaluates to this after side-effect