Skip to content

Instantly share code, notes, and snippets.

@benhoskings
Forked from lachie/gist:68752
Created February 23, 2009 02:22
Show Gist options
  • Save benhoskings/68755 to your computer and use it in GitHub Desktop.
Save benhoskings/68755 to your computer and use it in GitHub Desktop.
#Ruby assignment option to assign unless nil
replacement_content = page_contents.content_label_find(item)
##########################################################
# The following is wrong, see http://gist.github.com/68772
##########################################################
# This
element.inner_html = replacement_content if replacement_content
# doesn't do what you expect. It always assigns element.inner_html,
# even if replacement_content is nil.
# It's equivalent to:
b = (a if a)
# If a is nil, the expression (a if a) evaluates to nil, which is assigned to b.
# Also this isn't what you want, but remember that there's &&= as well as ||=.
a = nil # => nil
b = 'lol' # => "lol"
a &&= b # => nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment