Skip to content

Instantly share code, notes, and snippets.

@gboone
Created January 20, 2016 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gboone/5f2562564c9dfddf38f8 to your computer and use it in GitHub Desktop.
Save gboone/5f2562564c9dfddf38f8 to your computer and use it in GitHub Desktop.
Today I Learned about ruby logic
# this:
if expected['value']
variable = expected['value']
else
variable = "default"
end
# is the same as this:
unless variable = expected['value']
variable = "default"
end
###
# In the first example, `variable` is set to `expected['value']` if `expected['value']`
# is set and return false if it's not. Returning `false` will, of course, send the
# program to the `else` condition, assigning `variable` to `"default"`.
#
# In the second it will set `variable` to `expected['value']` if it can, and "default"
# if the conditional evaluates `false`.
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment