Why does && in Ruby sometimes shortcut evaluates and sometimes doesnt?
I want to test if an element in a hash exists and if it is >= 0, then put
true or false into an array:
boolean_array << input['amount'] && input['amount'] >= 0
This raises no >= on NilClass error. However, if I just do this:
input['amount'] && input['amount'] >= 0 #=> false
No problem. Basically:
false && (puts 'what the heck?') #=> false
arr = []
arr << false && (puts 'what the heck?') #=> stdout: 'what the heck?'
arr #=> [false]
What gives?
No comments:
Post a Comment