Mar 31, 2007 @ 10:00 am

Unlike many other languages, 0 isn’t qualified as false in an expression. For example :

  1. a=0
  2. if(a)
  3.   #0 is true, so the code gets here
  4. end

To Ruby, only 2 things are false : false and nil. I should rather say : An instance of FalseClass and an instance of NilClass. Everything else is true. 0 is a Fixnum instance and therefore is considered to be true.

How ruby can return false when you write if(3>5) then? The answer is quite simple : It is false because 3>5 (or should I say 3.>(5)) returns an instance of FalseClass.

I thought it was something important to note as this unusual definition of truth can cause some headaches to the unwary…

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon
Rate this post :
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
Posted under : short & sweet
1 Comment
MyAvatars 0.2

Another thing to look out for is an empty string. Thats true also - unlike other scripting languages like perl, PHP, JavaScript etc.

Comment by : Binny V A
— April 7, 2007 @ 1:12 am




Leave a comment
Name (required)
Email (will not be publish) (required)
Website