Jamie Hill
Photo of Jamie Hill

UX Designer, CSS Wizard, Ruby on Rails Developer.

Ruby Ternary operator re-written with Boolean operators

Just noticed something interesting (well not that interesting). Ternary operators in Ruby can be re-written using Boolean operators e.g.

method = object.respond_to?(:foo) ? :foo : :bar

would become…

method = object.respond_to?(:foo) && :foo || :bar

Simply replace ? with && and : with ||
I don’t know if there is any performance gain here, anyone care to investigate?