Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Proposal for nil, 0, and "" in an if statement (http://www.velocityreviews.com/forums/t819930-proposal-for-nil-0-and-in-an-if-statement.html)

Dan Fitzpatrick 02-22-2005 11:49 AM

Proposal for nil, 0, and "" in an if statement
 
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





Bertram Scharpf 02-22-2005 12:28 PM

Re: Proposal for nil, 0, and "" in an if statement
 
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-scharpf.de



Dan Fitzpatrick 02-22-2005 12:56 PM

Re: Proposal for nil, 0, and "" in an if statement
 
--------------050603040304090209060501
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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




--------------050603040304090209060501--



David Heinemeier Hansson 02-22-2005 01:19 PM

Re: Proposal for nil, 0, and "" in an if statement
 
> 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.basecamphq.com/ -- Web-based Project Management
http://www.rubyonrails.org/ -- Web-application framework for Ruby
http://www.loudthinking.com/ -- Broadcasting Brain




Michael Neumann 02-22-2005 01:24 PM

Re: Proposal for nil, 0, and "" in an if statement
 
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




Pit Capitain 02-22-2005 01:41 PM

Re: Proposal for nil, 0, and "" in an if statement
 
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 02-22-2005 01:47 PM

Re: Proposal for nil, 0, and "" in an if statement
 
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 02-22-2005 02:10 PM

Re: Proposal for nil, 0, and "" in an if statement
 
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
--
(-, /\ \/ / /\/




David A. Black 02-22-2005 02:18 PM

Re: Proposal for nil, 0, and "" in an if statement
 
Hi --

On Tue, 22 Feb 2005, Gavin Kistner wrote:

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


It's arguably exactly the opposite of what you'd want -- namely, to
point up the singularity of 0/""/etc., not their ordinariness. 37534
is more vapid than 0 :-)

I'm not sold on this anyway. I think it would have the flavor of
"added on to the language as an afterthought". .zero? means the
object is zero, and .empty? means a container object is empty. I
don't think cross-breeding them retains the logic.


David

--
David A. Black
dblack@wobblini.net



Peter Hickman 02-22-2005 02:19 PM

Re: Proposal for nil, 0, and "" in an if statement
 
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.





All times are GMT. The time now is 01:03 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.