Home > database >  Ruby: Why does "and return" not return in this example?
Ruby: Why does "and return" not return in this example?

Time:10-25

def foo(x)
   puts "#{x} > 10" and return if x > 10
   puts "#{x} <= 10"
end

On the console:

> foo(3)
3 <= 10
> foo(30)
30 > 10
30 <= 10

CodePudding user response:

Kernel#puts returns nil, which is falsey. and and && only evaluate their right operand if they need to; since the left operand is already falsey, there is no need to evaluate the right operand.

  •  Tags:  
  • ruby
  • Related