0

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?

pinkfloyd90
  • 588
  • 3
  • 17
  • 1
    the block doesn't matter, it woudn't work outside of a block too. – Sergio Tulentsev Feb 23 '23 at 15:31
  • As an aside you could check this as follows: `Integer(value, exception: false)&.<(10)` – engineersmnky Feb 23 '23 at 15:35
  • 1
    Ruby's `and` and `or` are _control-flow operators_, e.g. `input = gets or fail("cannot read from stdin")`. This is why their precedence is (almost) as low as modifier-if. They are not meant as a "more readable" replacement for `&&` and `||` and you shouldn't use it as such. – Stefan Feb 23 '23 at 15:47

2 Answers2

4

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.

Tom Lord
  • 27,404
  • 4
  • 50
  • 77
  • Latest version of the `precedence` docs at the time of writing (not that any of it has actually changed): https://ruby-doc.org/3.2.1/syntax/precedence_rdoc.html – Tom Lord Feb 23 '23 at 15:29
  • Or the "auto redirect to the current version when you click the link" version: https://ruby-doc.org/current/syntax/precedence_rdoc.html – mu is too short Feb 23 '23 at 18:22
2

&& precedence is higher than and .That's why i will recommend using && instead of and

Your Solution is correct just add braces/parentheses () against Integer and remove the Space i.e value.is_a?(Integer)

hash = {a: 5, b: 10}
hash.all?{|key,value| value.is_a?(Integer) && value < 10}

#=> false