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

B

Brian Mitchell

In fact, those are actual symbols and not numbers!

exactly! This issue seems to come up on the ML from time to time. I
voiced my opinion that Smalltalk conditionals (depending on behavior)
are much better than depending on value (check for nil or false). It
can be tailored to fit any object easily... but I think the idea lost
out to people who would rather keep ruby more of that same and
probably a little faster (though there are some things ruby can't do
because of this). My vote is still up for .true? {} and .false? {}

;-)

I really don't care if you guys agree or not but it sure is elegant.
Ruby is already a very good language so I am happy anyhow.
Regards,

Michael

Brian.
 
M

Michael Neumann

Brian said:
exactly! This issue seems to come up on the ML from time to time. I
voiced my opinion that Smalltalk conditionals (depending on behavior)
are much better than depending on value (check for nil or false). It
can be tailored to fit any object easily... but I think the idea lost
out to people who would rather keep ruby more of that same and
probably a little faster (though there are some things ruby can't do
because of this). My vote is still up for .true? {} and .false? {}

;-)

I really don't care if you guys agree or not but it sure is elegant.
Ruby is already a very good language so I am happy anyhow.

At least for .nil? I agree. I am not sure whether I'd like to write
"if a.true?" all the time. Nevertheless I believe that

if a.false?

is clearer to read and understand than:

if not a

Just because human mind if very bad at negating conditions (proven by
studies of some psychologists).

Regards,

Michael
 
H

Hal Fulton

Gavin said:
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.

FWIW, I once suggested the name "null?" for this.

I've always been perturbed that there is no good word which
means non-nil or non-null. A positive should be representable
as something other than a double negative IMO.


Hal
 
R

Robert Klemme

Michel Martens said:
Even though I'm not sure I love globals like $\ or $*, I think the
"rule of validity" method should depend of a variable of that kind.
For instance, a global $- could be defined
and default to [nil, false, "", 0, [], {}]. That way, one could define
at runtime wich values can be declared "vapid" or "empty" or whatever
the final method name is.

I'm not sure this is a good idea as this seems likely to make all sorts of
library code behave in strange ways if they use the new method.

Kind regards

robert
 
N

Neil Stevens

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:

I get so disappointed when I see so many people so eager to break existing
Ruby code.

Part of developing a language is keeping up the responsibility you have to
people who took you up on your advice and started using the language. I
hope that the chief Ruby decision makers remember that, and shun proposals
like this which will subtlely break lots of existing Ruby software.

Ruby is not C. It's not hard to write '!= 0'. Just because a lot of C
developers say it's bad to do that in C, it doesn't follow that such a
thing is bad in every language. So, there's no reason to make a massive
break like this. Please don't do it.
 
J

Jeremy Henty

class NilClass
def empty?
true
end
end

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

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

Please, no! "empty? => true" means "I am a container, but I have no
elements". Fixnums are not containers. Booleans are not containers.
They should not respond_to?:)empty).

Cheers,

Jeremy Henty
 
J

Jeremy Henty

We're ... simply stating that [the value is] one that we commonly
aren't interested in.

Which is *precisely* why it's a bad idea. It's *not* common to be
uninterested in nil. If you're calling a method that normally returns
an Array or a Fixnum you should be *very* interested indeed if it
instead returns "nil". Silently coercing nil to some other value
(which is what this proposal in practice amounts to) is begging for
bugs.

Remember the Pragmatic Programmers' maxim: Ruby's virtue is that it
doesn't get in your way. Making the common case (ie. nil should be
treated specially) harder to code without bugs solely to make an
uncommon case easier is *obstructive*. It's not the Ruby way.

Cheers,

Jeremy Henty
 
J

Jeremy Henty

Michel Martens said:
For instance, a global $- could be defined and default to [nil,
false, "", 0, [], {}]. That way, one could define at runtime wich
values can be declared "vapid" or "empty" or whatever the final
method name is.

Oh no! Let's introduce a hook so that *anyone* can break *any* code
that uses these methods! Even *Perl* shuns this sort of "action at a
distance"! :) Please don't bring it into Ruby!

Jeremy Henty
 
D

Dave Burt

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

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

What about other (uninitialized) objects?
S = Struct.new:)foo, :bar)
s = S.new
s.empty? #=> true
s.foo = 0
s.empty? #=> false

Name suggestions (Thanks Roget):
#insignificant?
#paltry?
#petty?
#trivial?
#unsubstantial?
#negligible?
#base?
#void?
#plain?
#to_b

or, for the opposite operation:
#has_value?

I agree with Gavin and others that cases regularly pop up where this would
be useful

Cheers,
Dave
 
M

Martin DeMello

Dave Burt said:
Name suggestions (Thanks Roget):
#insignificant?
#paltry?
#petty?
#trivial?
#unsubstantial?
#negligible?
#base?
#void?
#plain?
#to_b

#pseudonull?

martin
 
M

Mathieu Bouchard

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

In Cantor-style Set Theory, the Naturals usually get redefined in such a
way that 0 is equal to the empty set. Which means, incidentally, that 0
_is_ the empty set, and vice versa.

That definition is quite popular in Logic, but fails to draw much usage in
the rest of Mathematics. It's not the only definition either. Dedekind's
definition implies 0 is the set of all rationals lower than 0. Those
multiple defs can be thought of as different implementations, while the
usually-used interface consists of the operators +,-,*,/,etc.

In computer science, the first definition would be the most accepted of
the two, simply because the latter involves Real Numbers, which don't
exist in reality, by Skölem's Paradox. This may exclude the many lost
souls who don't get math, such Niklaus Wirth, who gave the name of Real to
a floating-point type (!!!) in PASCAL.

(If you think some programmers have bad naming skills when it comes to
writing their programs, find who called the Real Numbers "Real", when
almost all elements of that set don't exist.)

_____________________________________________________________________
Mathieu Bouchard -=- Montréal QC Canada -=- http://artengine.ca/matju
 
C

Csaba Henk

In computer science, the first definition would be the most accepted of
the two, simply because the latter involves Real Numbers, which don't
exist in reality, by Skölem's Paradox. This may exclude the many lost
souls who don't get math, such Niklaus Wirth, who gave the name of Real to
a floating-point type (!!!) in PASCAL.

That guy who has a paradox, is simply Skolem, without the umlaut.

But, afaik, Skolem paradox is the phenomena that (provided set theory is
consistent) it has a countable model; on the other hand, set theory
knows of arbitrarily large cardinalities -- how can all that stuff fit
into a simple countable universe?

I wouldn't even call it a paradox: if you know the proper meaning of the
notions being involved, you'll see how it's possible. It just sounds
weird.

Comme ci, comme ca, how can you arrive to ontological claims from this
point? Speaking of existence is a marshy area in mathematics everywhere
beyond finiteness. In what sense does the set of all natural numbers
exist? From the consensual statement that 1, 2, 3, and so on, all exist,
you can't just jump there and claim, "there is a set of all natural
numbers". Similarly you can ask, in what sense do real numbers and their
set exist?

Note that again, existence of individual reals and their set is quite a
different problem. At least, the existence of their set. Denying the
latter doesn't imply denying the former. Intutively, a real number is
at about the same level of complexity as the set of natural numbers (as
a real between 0 and 1 can be specified by a set of natural numbers: the
indices where you have 1 in its binary representation; it's easy to
extend to all reals); but their set it at "one level higher".

All these considerations don't give an answer to the questions of
existence. In fact, what can answer to such a question is nothing but
your preconceptions :) And that's fine, they are just better kept far
from mathematics. Mathematicians don't need anything non-consensual
involved into the game. This is why you don't hear them speaking up in
such issues.

Csaba
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top