Skip to content

Instantly share code, notes, and snippets.

@coreyti
Created April 23, 2012 03:25
Show Gist options
  • Save coreyti/2468664 to your computer and use it in GitHub Desktop.
Save coreyti/2468664 to your computer and use it in GitHub Desktop.
`defined?` as conditional expression in two rubies
# ruby 1.8.7
if defined? THOUGHT ; defined?(self) ; end # returns nil
if defined? THOUGHT && THINK ; defined?(self) ; end # returns nil
if defined?(THOUGHT) && THINK ; defined?(self) ; end # returns nil
if defined?(THOUGHT && THINK) ; defined?(self) ; end # returns nil
defined?(THOUGHT && THINK) # returns nil
# ruby 1.9.3
if defined? THOUGHT ; defined?(self) ; end # returns nil
if defined? THOUGHT && THINK ; defined?(self) ; end # returns "self"
if defined?(THOUGHT) && THINK ; defined?(self) ; end # returns nil
if defined?(THOUGHT && THINK) ; defined?(self) ; end # returns "self"
defined?(THOUGHT && THINK) # returns "expression"
# hmm... it occurs to me that i'm being a bit too cute above.
# an example of where this might be a real issue follows.
# from the oauth-plugin lib at https://github.com/pelle/oauth-plugin:
if defined? OAUTH_10_SUPPORT && OAUTH_10_SUPPORT
# do OAuth 1.0 stuff (maybe not in ruby 1.8, but **always** in 1.9)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment