[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Proposal for nil, 0, and "" in an if statement

Dan Fitzpatrick

2/22/2005 11:50:00 AM

The following was derived from a portion of the destrutive! operations
thread.

Here is a proposal for evaluating "", 0, and nil in an if statement:

Add empty? to NilClass and Fixnum

class NilClass
def empty?
true
end
end

class Fixnum
def empty?
self == 0 ? true : false
end
end

nil.empty? => true
0.empty? => true

These already exist:
"".empty? => true
[].empty? => true
{}.empty? => true

Dan




45 Answers

Bertram Scharpf

2/22/2005 12:28:00 PM

0

Hi,

Am Dienstag, 22. Feb 2005, 20:49:51 +0900 schrieb Dan Fitzpatrick:
> The following was derived from a portion of the destrutive! operations
> thread.
>
> Here is a proposal for evaluating "", 0, and nil in an if statement:
>
> Add empty? to NilClass and Fixnum
>
> nil.empty? => true
> 0.empty? => true
>
> These already exist:
> "".empty? => true
> [].empty? => true
> {}.empty? => true

Maybe even "true.empty? #=> true", "false.empty? #=> false"?

A good chance to end the discussions started over and over
about strings and numbers in `if' clauses, I think.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...


Dan Fitzpatrick

2/22/2005 12:56:00 PM

0

Bertram Scharpf wrote:

>Hi,
>
>Am Dienstag, 22. Feb 2005, 20:49:51 +0900 schrieb Dan Fitzpatrick:
>
>
>>The following was derived from a portion of the destrutive! operations
>>thread.
>>
>>Here is a proposal for evaluating "", 0, and nil in an if statement:
>>
>>Add empty? to NilClass and Fixnum
>>
>>nil.empty? => true
>>0.empty? => true
>>
>>These already exist:
>>"".empty? => true
>>[].empty? => true
>>{}.empty? => true
>>
>>
>
>Maybe even "true.empty? #=> true", "false.empty? #=> false"?
>
>A good chance to end the discussions started over and over
>about strings and numbers in `if' clauses, I think.
>
>Bertram
>
>
>
>
I would reverse the true and false:

true.empty? => false
false.empty? => true

These are not quite as intuitive as NilClass and Fixnum but I agree they would be nice to have as well.

Dan



David Heinemeier Hansson

2/22/2005 1:19:00 PM

0

> Here is a proposal for evaluating "", 0, and nil in an if statement:

I'd be pretty happy to see that happen. It would make especially Ruby
in view logic a lot easier to deal with.
--
David Heinemeier Hansson,
http://www.basec... -- Web-based Project Management
http://www.rubyon... -- Web-application framework for Ruby
http://www.loudthi... -- Broadcasting Brain



Michael Neumann

2/22/2005 1:24:00 PM

0

David Heinemeier Hansson wrote:
>> Here is a proposal for evaluating "", 0, and nil in an if statement:
>
>
> I'd be pretty happy to see that happen. It would make especially Ruby in
> view logic a lot easier to deal with.

But please please use a different name than "empty?", maybe "nothing?"
or "something?" or whatever else.

Regards,

Michael



dblack

2/22/2005 1:24:00 PM

0

Pit Capitain

2/22/2005 1:41:00 PM

0

David Heinemeier Hansson schrieb:
>> Here is a proposal for evaluating "", 0, and nil in an if statement:
>
>
> I'd be pretty happy to see that happen. It would make especially Ruby in
> view logic a lot easier to deal with.

Could anyone post examples of actual code that would be easier if 0 had a
special property in common with nil and ""? I think I never needed this in Ruby.

Regards,
Pit


Gavin Kistner

2/22/2005 1:48:00 PM

0

On Feb 22, 2005, at 6:41 AM, Pit Capitain wrote:
> David Heinemeier Hansson schrieb:
>>> Here is a proposal for evaluating "", 0, and nil in an if statement:
>> I'd be pretty happy to see that happen. It would make especially Ruby
>> in view logic a lot easier to deal with.
>
> Could anyone post examples of actual code that would be easier if 0
> had a special property in common with nil and ""? I think I never
> needed this in Ruby.

I don't have actual code, but imagine looping through records returned
with financial data, and you want to print out a "-" if the field is
missing -or- zero. The whole "this field doesn't have any meaningful
information that contributes to the sum of the column" spreadsheet
thing.

I would support an RCR with this proposal, under just about any method
name :)
And I agree, boolean types need the same method as well.



Gavin Kistner

2/22/2005 2:10:00 PM

0

On Feb 22, 2005, at 6:47 AM, Gavin Kistner wrote:
> I would support an RCR with this proposal, under just about any method
> name :)

As I responded in the destructive! thread, if people are opposed to the
term "empty?", then what about the sort of fun name of "vapid?"
instead?

vap-id
adj.
1. Lacking liveliness, animation, or interest; dull: vapid
conversation.
2. Lacking taste, zest, or flavor; flat: vapid beer.

We're not actually denigrating the value so far as to call it empty or
useless, but simply stating that it's one that we commonly aren't
interested in. (Although to be precise, I suppose we tend to be
interested in these values specifically because they are so different
from all the others...it's just that it's only the others that we care
about :)

0.vapid? #=> true
"".vapid? #=> true
[].vapid? #=> true
{}.vapid? #=> true
false.vapid? #=> true
nil.vapid? #=> true
--
(-, /\ \/ / /\/



dblack

2/22/2005 2:19:00 PM

0

Peter Hickman

2/22/2005 2:19:00 PM

0

Gavin Kistner wrote:

> I don't have actual code, but imagine looping through records returned
> with financial data, and you want to print out a "-" if the field is
> missing -or- zero. The whole "this field doesn't have any meaningful
> information that contributes to the sum of the column" spreadsheet thing.
>
Might this not cause trouble with databases. A database record (returned
as a structure or object) would have NULL == nil for fields with no
value which is not the same as a field with a definite value such as 0.