From: Michael Nissim Date: 2010-07-15T14:45:22+09:00 Subject: Re: differnce between .nil? , .empty?, .blank? Xavier Noria wrote: > On Thu, Jul 24, 2008 at 9:00 AM, Sijo Kg wrote: > >> Could you please tell me when to use .nil? , .empty?, .blank? .What >> are the difference between them.. For example I have >> params[:company][:whichCompany] >> And to check for it is null I first attempted all these and finally the >> following worked >> if !params[:company][:whichCompany].empty? >> >> So now really i am confused .Please tell me the differnce > > nil? tests whether the object is exactly nil, that is whether it is > the one and only want instance of NilClass. > > empty? is a method some objects respond to. You need to check the > documentation for each case. For example, and empty array is one that > is not nil (it is an array right?) and has no elements. An empty > string is one that is not nil (it is a string right?) and has no > bytes, nothing. > > The blank? method you ask for does not belong to Ruby, it is a Rails > extension: http://api.rubyonrails.com/classes/Object.html#M000011. This is answer was concise and helpful. Thanks! I personally find this confusing: >> a[:real]=false >> a[:real] => false >> a[:real].blank? => true ?? if I populated a[:real] with false, why does '.blank?' return true? That's confusing. But hey, if you can't win them, join them. I love Ruby and Rails! -- Posted via http://www.ruby-forum.com/.