bluejimmy bluejimmy - 3 months ago 10
Ruby Question

What does || between methods mean?

def respond_to?(method, include_private = false)
super || @subject.respond_to?(method, include_private)
end


||
is an
or
operator, so what does
||
between methods mean?

Will it always call
super
unless
super
returns nil then evaluate
@subject.respond_to
because of short-circuiting for the
||
operator?

Edit: I think my question is not a duplicate because I know how the '||' operator work from other programming languages. I know what short-circuiting is. I just have never seen it used with just two methods without if statements involve.

Answer

The || operator means the same regardless of how complex the expressions are on each side of it.

A || B means:

  • evaluate A
  • if A == false or A == nil
    • evaluate B
    • return the value of B as the value of the A || B expression
  • otherwise return the value of A as the value of the A || B expression
Comments