-
-
Save benhoskings/68755 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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