Skip to content

Instantly share code, notes, and snippets.

@colinrymer
Created September 20, 2013 18:14
Show Gist options
  • Save colinrymer/6641528 to your computer and use it in GitHub Desktop.
Save colinrymer/6641528 to your computer and use it in GitHub Desktop.
Ternary Shortcut Proposal

Basis

Currently the convention is for methods ending in ? to return a boolean value. Often this method is used at the beginning of a ternary operator, e.g. protocol = request.ssl? ? 'https' : 'http'.

Proposal

The proposed would be to allow the ...? method to accept two arguments, returning the first if the method would return true, the second if false. This may not follow the principal of least surprise, so another proposed alternative would be suffixing the method with ??.

Examples

# The following are equivalent to
# `protocol = request.ssl? ? 'https' : 'http'`

protocol = request.ssl?('https', 'http')

# or

protocol = request.ssl??('https', 'http')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment