Why does && in Ruby sometimes shortcut evaluates and sometimes doesnt? -
i want test if element in hash exists , if >= 0, put true or false array:
boolean_array << input['amount'] && input['amount'] >= 0
this raises no >= on nilclass error. however, if this:
input['amount'] && input['amount'] >= 0 #=> false
no problem. basically:
false && (puts 'what heck?') #=> false arr = [] arr << false && (puts 'what heck?') #=> stdout: 'what heck?' arr #=> [false]
what gives?
currently it's being grouped as:
(boolean_array << input['amount']) && input['amount'] >= 0
try:
boolean_array << (input['amount'] && input['amount'] >= 0)
however, if ends being false, expression returns nil
, want:
boolean_array << (!input['amount'].nil? && input['amount'] >= 0)
Comments
Post a Comment