From: Peter Vandenabeele Date: 2011-09-24T20:34:05+09:00 Subject: Re: Conditional statements with multiple arguments --0016e6506fd687647404adae4dd4 Content-Type: text/plain; charset=UTF-8 On Sat, Sep 24, 2011 at 1:19 PM, Thomas B. wrote: > Peter, > > Is there a reason that something like an empty array or an empty string > would evaluate to true as opposed to nil? Or is it just one of those > things that just is the way it is? That's it indeed ... in more detail, only the singleton objects of NilClass and FalseClass are false, all the rest is true. peterv@ASUS:~$ ruby -v ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux] peterv@ASUS:~$ irb 001:0> false.class => FalseClass 002:0> nil.class => NilClass I guess I just don't understand the > purpose of Ruby handling an empty array the same way as it would handle > an array with a given value. Wouldn't it just be easier to have the > empty array evaluate to nil like this: > > array = [] > > puts array.nil? # returns => true (it actually returns false, because as > you said, it isn't nil or false) > > array = [1, 2] > > puts array.nil? # returns => false > > I know we're getting a little bit into the bushes with this stuff, and I > should probably be a little bit more resourceful. It's just interesting > to me that an empty array or string would evaluate to a true statement. > On Array and String, you could use empty? to that purpose: 006:0> a = Array.new => [] 007:0> a.empty? => true 008:0> a << "test" => ["test"] 009:0> a.empty? => false ... 012:0> s = String.new => "" 013:0> s.empty? => true 014:0> s << "test" => "test" 015:0> s.empty? => false But then again, this is an interesting case: 016:0> s = " " => " " 017:0> s.empty? => false 018:0> s.strip.empty? => true All of this can be found in basic Ruby tutorials... HTH, Peter --0016e6506fd687647404adae4dd4--