Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading