(1..5).reduce do |sum, n|
sum + n if n.even?
end
This code will break. Why ?
1.when n = 1, the if statement is false. The whole statement returns nil.
2. nil returns to sum.
3. when n = 2 , sum + 2 means nil + 2 . Which will churn out an error.
sum + n if n.even?
end
This code will break. Why ?
1.when n = 1, the if statement is false. The whole statement returns nil.
2. nil returns to sum.
3. when n = 2 , sum + 2 means nil + 2 . Which will churn out an error.
Comments
Post a Comment