I am a newbie programming Ruby. I have the below code. Method sum_to_n? which takes an array of integers and an additional integer, n, as arguments and returns true if any two elements in the array of integers sum to n. It should return true for the empty array with zero argument, but keeps returning false.
def sum_to_n? (arr, n)
hash = Hash.new(0)
arr.each do |val|
if hash.key? val
return true
else
hash[n-val] = val
end
end
return false
end
What am I doing wrong ?