From: "Guy N. Hurst" Date: 2001-06-16T18:04:03+09:00 Subject: [ruby-talk:16533] Re: why only nil and false are regardedas false? Steven Haryanto wrote: > ... > >Do you have some examples where you'd like other values to be false? > > So far my mind has been fitted with these idioms: > > # if string s is empty > if (not s) ... > if s.empty? then .... end # assumes you know s is a string > # if number is zero > if (not n) ... > if n.zero? then .... end # assumes you know n is a number > # if there are no elements in array a > if (not a) ... > if a.empty? then .... end # assumes you know a is an array > I believe this idiom goes as far back as C and perhaps even further. > However, in Ruby they will all be wrong. What are the preferred way > to do this? > As above. Note that Ruby variables refer to objects of a specific class, and you must pass methods that you know the referenced object can handle - or you'll get an error. There are ways to deal with this, though. Guy N. Hurst -- HurstLinks Web Development http://www.hurstlinks.com/ Norfolk, VA 23510 (757)623-9688 FAX 623-0433 PHP/MySQL - Ruby/Perl - HTML/Javascript