Hi all. I want to use if-else condition in one line. I have used the ternary operator, but doesn't seem to work. Any clues?
class Array
def painful_injection
each do |item|
sum = yield (defined?(sum).nil?) ? 0 : sum, item #pass the arguments to the block
end
sum
end
end
puts [1, 2, 3, 4].painful_injection {|sum, nxt_item| sum + nxt_item}
This gives me an error:
Error :undefined method `+' for false:FalseClass