From: Michael Nissim on
Xavier Noria wrote:
> On Thu, Jul 24, 2008 at 9:00 AM, Sijo Kg <sijo(a)maxxion.com> 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/.

From: Xavier Noria on
On Thu, Jul 15, 2010 at 7:45 AM, Michael Nissim <info(a)israelunique.com> wrote:

> 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!

It should work

$ script/console
Loading development environment (Rails 2.3.2)
>> a = {}
=> {}
>> a[:real] = false
=> false
>> a[:real].blank?
=> true

false is blank?, no matter where it is stored.