[Note: parts of this message were removed to make it a legal post.]
1) In Ruby, false and nil are treated as falsy. Everything else is treated
as truthy.
"truthy" if nil # => nil
"truthy" if false # => nil
"truthy" if true # => "truthy"
"truthy" if "str" # => "truthy"
"truthy" if 1 # => "truthy"
"truthy" if 1.23 # => "truthy"
"truthy" if /regex/ # => nil
"truthy" if :symbol # => "truthy"
"truthy" if 'r'..'ange' # => "truthy"
I just listed out a bunch of examples and then ran them.... But I'm looking
now, and realize that /regex/ evaluated as falsy! After some playing
around, I have decided that it is based on Perl. If you put a regex in a
variable, it treats it as a boolean "is it an object?" and behaves as I
stated previously. But if you put it in a literal, it plays Perl and checks
to see if it matches against $_
regex = /re(.)ex/
$_ # => nil
# in literal, so checks if it matches $_
# (note that it warns: regex literal in condition)
"truthy" if /re(.)ex/ # => nil
# in var, so checks if it exists
"truthy" if regex # => "truthy"
# explicitly set $_, now literal matches
$_ = "reGex"
"truthy" if /re(.)ex/ # => "truthy"
# when we look at capture group, we see the G we made
$1 # => "G"
# change the $_ to see if variable matches it
$_ = "reXex"
"truthy" if regex # => "truthy"
# nope, it should be "X" if it matched.
$1 # => "G"
# just to double check
$1 if /re(.)ex/ # => "X"
So, regex aren't nil or false, and are thus truthy. But when you throw a
regex literal in the conditional, it does some implicit operations and
doesn't pass the regex straight on through to the conditional.