From: Josh Cheek Date: 2011-04-04T18:58:40+09:00 Subject: Re: pipe question --20cf3054ab87b8f27c04a014cda0 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Apr 3, 2011 at 10:42 PM, Josh Cheek wrote: > 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. --20cf3054ab87b8f27c04a014cda0--