Search code examples
rubyhashoperatorsoperator-precedence

Why does && operator not work inside a hash block but and operator does?


Given the following hash

hash = {a: 5, b: 10}

I want to check if all values are Integers and < 10.

hash.all?{|key,value| value.is_a? Integer && value < 10} ## TypeError: class or module required from (pry):3:in `is_a?'

hash.all?{|key,value| value.is_a? Integer and value < 10} ## false

Why does the first example with the && operator not work inside the block? Is this a precedence issue?


Solution

  • Is this a precedence issue?

    Yes. The first form is being evaluated like this:

    value.is_a?(Integer && (value < 10))
    

    And the second form is being evaluated like this:

    value.is_a?(Integer) and (value < 10)
    

    Therefore the first form is actually running an unexpected/incorrect operation - either:

    value.is_a?(true)
    # or
    value.is_a?(false)
    

    There are many ways to write this, but I would do it as:

    value.is_a?(Integer) && value < 10
    

    In general, completely omitting brackets in complex statements like the above is asking for trouble, and I'd advise avoiding it. It's easy to fall into traps like this, where your code isn't executing in the order you intended.