Why is `return a or b` a void value expression error in Ruby?

后端 未结 3 1289
长情又很酷
长情又很酷 2020-12-15 18:15

This is just fine:

def foo
  a or b
end

This is also fine:

def foo
  return a || b
end

This returns

3条回答
  •  温柔的废话
    2020-12-15 19:04

    return a or b is interpreted as (return a) or b, and so the value of return a is necessary to calculate the value of (return a) or b, but since return never leaves a value in place (because it escapes from that position), it is not designed to return a valid value in the original position. And hence the whole expression is left with (some_void_value) or b, and is stuck. That is what it means.

提交回复
热议问题