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

bluejimmy
  • 390
  • 6
  • 14
  • Yep, that's basically correct. Except that `false` is a falsey value too. – Sergio Tulentsev Aug 26 '16 at 09:06
  • That is correct, if the inherited/parent class does define respond_to or if it returns nil or false, then `@subject.respond_to?` would be evaluated. – codyeatworld Aug 26 '16 at 09:06
  • Possible duplicate of [Understanding the "||" OR operator in If conditionals in Ruby](http://stackoverflow.com/questions/1554340/understanding-the-or-operator-in-if-conditionals-in-ruby) – halfelf Aug 26 '16 at 09:18

1 Answers1

4

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
Andrey Deineko
  • 51,333
  • 10
  • 112
  • 145
sokkyoku
  • 2,161
  • 1
  • 20
  • 22