Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Proposal for nil, 0, and "" in an if statement

Reply
Thread Tools

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

 
 
Michael Neumann
Guest
Posts: n/a
 
      02-22-2005
David A. Black wrote:
> 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.


100% agree. I especially cannot understand (at least mathematically) why
0 would be empty and 1 not.

Why not simply define a has_value? method:

def has_value?(obj)
case obj
when Enumerable
!obj.empty?
when NilClass, FalseClass
false
when TrueClass
true
when Integer
!obj.zero?
else
# do what you want
obj.respond_to?(...)
...
end
end

This probably serves most needs...

Regards,

Michael


 
Reply With Quote
 
 
 
 
Gavin Kistner
Guest
Posts: n/a
 
      02-22-2005
On Feb 22, 2005, at 7:32 AM, Gavin Kistner wrote:

> On Feb 22, 2005, at 7:19 AM, Peter Hickman 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.

>
> Yes, there are many cases where you DO want to distinguish between nil
> and 0. These cases are already handled by the current behavior of
> Ruby.


To be clear - the proposal is NOT to make Ruby act like one of the many
other languages where "", 0 and friends are treated as non-truth values
in boolean expressions. The proposal is simply to add an additional
method to all that allows a programmer to treat them the same in the
few cases where that is desirable.



 
Reply With Quote
 
 
 
 
Peter Hickman
Guest
Posts: n/a
 
      02-22-2005
Gavin Kistner wrote:

> To be clear - the proposal is NOT to make Ruby act like one of the
> many other languages where "", 0 and friends are treated as non-truth
> values in boolean expressions. The proposal is simply to add an
> additional method to all that allows a programmer to treat them the
> same in the few cases where that is desirable.
>

As long as we are only proposing to add a new method then I am happy but
to be honest I think that the utility of this method will be very small
and the potential for confusion big. The difference will be subtle and
only visible in particular situations. I expect that it could become a
gotcha very quickly.



 
Reply With Quote
 
Nikolai Weibull
Guest
Posts: n/a
 
      02-22-2005
* Dan Fitzpatrick (Feb 22, 2005 14:00):
> 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.


How is 0 empty? 0 is a number representing nothing; it's not empty.

How is nil empty? It's nothing, thus it can't be empty.

A string is a container of symbols/character. A string containing no
symbols/characters is empty.

An array or a hashtable is a container of elements/items. An array or a
hashtable containing no elements/items is empty.

Stop equating [] and nil. Ruby is thankfully not PHP,
nikolai

--
::: name: Nikolai Weibull :: aliases: pcp / lone-star / aka :::
::: born: Chicago, IL USA :: loc atm: Gothenburg, Sweden :::
::: page: www.pcppopper.org :: fun atm: gf,lps,ruby,lisp,war3 :::
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}


 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      02-22-2005
On Tue, 22 Feb 2005 22:24:18 +0900, Michael Neumann <(E-Mail Removed)> wrote:
> 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.


I think it was a joke, but why not #vapid? -- it came from the spawning thread.

-austin
--
Austin Ziegler * http://www.velocityreviews.com/forums/(E-Mail Removed)
* Alternate: (E-Mail Removed)


 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      02-22-2005
Gavin Kistner schrieb:
> 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.


When dealing with financial data, you often have to calculate the product of a
couple of values: v1 * v2 * v3 * ... Here, the number, that "doesn't have any
meaningful information that contributes to the PRODUCT of the column", is 1. I
think you can find many examples where the number 0 is special, but you can also
find examples where another number is special.

Regards,
Pit


 
Reply With Quote
 
Kaspar Schiess
Guest
Posts: n/a
 
      02-22-2005
(In response to news:(E-Mail Removed) by
Gavin Kistner)

> To be clear - the proposal is NOT to make Ruby act like one of the many
> other languages where "", 0 and friends are treated as non-truth values
> in boolean expressions. The proposal is simply to add an additional
> method to all that allows a programmer to treat them the same in the
> few cases where that is desirable.


I can't see why this method should be added to all of Ruby.. it seems to
apply to a special case you need to handle. You know, it is perfectly ok to
change stdlib just for your project... this is what Ruby is about.

I feel very at home with the distinction between empty and nil that is made
currently. In the case of financial data that may have empty cells I would
propose to add a small class layer between that has an empty? method.. thus
solving the problem in an app specific way and not changing the language.

kaspar

--
Keep Ruby Clean Initiative



 
Reply With Quote
 
Austin Ziegler
Guest
Posts: n/a
 
      02-22-2005
On Wed, 23 Feb 2005 00:32:02 +0900, Kaspar Schiess <(E-Mail Removed)> wrote:
> I can't see why this method should be added to all of Ruby.. it seems to
> apply to a special case you need to handle. You know, it is perfectly ok to
> change stdlib just for your project... this is what Ruby is about.
>
> I feel very at home with the distinction between empty and nil that is made
> currently. In the case of financial data that may have empty cells I would
> propose to add a small class layer between that has an empty? method.. thus
> solving the problem in an app specific way and not changing the language.


This, or something like it, would be useful for the case where I have to do:

foo if bar.nil? or bar.empty?
foo(bar) if bar and not bar.empty?

It's not common, but it is common enough. I want to sometimes write it as:

foo if bar.empty?

...because for the purposes of what I'm doing (I think that this is in
Text::Format), +nil+ is as good as empty.

-austin
--
Austin Ziegler * (E-Mail Removed)
* Alternate: (E-Mail Removed)


 
Reply With Quote
 
Jon A. Lambert
Guest
Posts: n/a
 
      02-22-2005
Michael Neumann wrote:
>
> But please please use a different name than "empty?", maybe "nothing?"
> or "something?" or whatever else.
>


How about "piningforcplusplus?"

J. Lambert



 
Reply With Quote
 
Michael Neumann
Guest
Posts: n/a
 
      02-22-2005
Caio Tiago Oliveira wrote:
> Michael Neumann, 22/2/2005 11:25:
>> 100% agree. I especially cannot understand (at least mathematically)
>> why 0 would be empty and 1 not.
>> (...)

>
>
> Binary logic, 0 is false, 1 is true.


In fact, those are actual symbols and not numbers!

Regards,

Michael


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
Which of switch statement and if-else statement takes less time to execute? swaroophr@gmail.com C Programming 21 08-02-2005 09:24 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM
Proposal: runtime validation statement Paul Rubin Python 11 07-14-2004 12:47 PM



Advertisments