[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

nil != []

alex

1/28/2006 8:51:00 PM

OK. Here's my second stupid question for today.

What's the rational behind having 0, [] and "" evaluate to true? ""
and [], I could kind of see. But 0, how on Gods earth can 0 true?

alex

--
Alex Polite
http://flo... - finding the right open source


16 Answers

Vince Puzzella

1/28/2006 8:56:00 PM

0

I'm new to ruby but this makes sense to me. Why should 0 == false? It's a
valid FixNum.

On 1/28/06 3:51 PM, "Alex Polite" <notmyprivateemail@gmail.com> wrote:

> OK. Here's my second stupid question for today.
>
> What's the rational behind having 0, [] and "" evaluate to true? ""
> and [], I could kind of see. But 0, how on Gods earth can 0 true?
>
> alex
>
> --
> Alex Polite
> http://flo... - finding the right open source
>




Simon Kröger

1/28/2006 9:06:00 PM

0

Alex Polite wrote:
> OK. Here's my second stupid question for today.
>
> What's the rational behind having 0, [] and "" evaluate to true? ""
> and [], I could kind of see. But 0, how on Gods earth can 0 true?
>
> alex
>
> --
> Alex Polite
> http://flo... - finding the right open source

how on earth can -1 be true?

No special reason, it's just that way. use zero? if you want to know if
it's zero. (What bugs you is the low level knowledge (or better an idea)
on how 0 and false would be represented in memory - Qnil is a 4 in ruby :))

cheers

Simon

btw: i love pythons way in this case



Matthew Smillie

1/28/2006 9:08:00 PM

0

On Jan 28, 2006, at 20:51, Alex Polite wrote:

> OK. Here's my second stupid question for today.
>
> What's the rational behind having 0, [] and "" evaluate to true? ""
> and [], I could kind of see. But 0, how on Gods earth can 0 true?

0 *the integer* is only false by convention, and it's a convention
confined to programming, originating (unless I'm mistaken) from
languages which didn't define specific 'true' and 'false' logcial
values separate from integer math. 0's used in some logical
notations as a symbol for 'false', but it's unlikely that anyone
familiar with formal logic will tell you those 0's are the same 0's
you get from "2 - 2".

There's no doubt that the convention's been made very useful, but
there's really no logical basis for equating any particular symbol to
true or false truth values over any other.

matthew smillie.


alex

1/28/2006 9:40:00 PM

0

On 1/28/06, Matthew Smillie <M.B.Smillie@sms.ed.ac.uk> wrote:
> On Jan 28, 2006, at 20:51, Alex Polite wrote:
>
> > OK. Here's my second stupid question for today.
> >
> > What's the rational behind having 0, [] and "" evaluate to true? ""
> > and [], I could kind of see. But 0, how on Gods earth can 0 true?
>
> 0 *the integer* is only false by convention,

Well I do understand that it's a convention. They same is true for a
lot of things. Like the positioning of gas and break pedals in cars.
It's just a convention. But when GM puts a new car in the market they
don't swap them. My question is why the constructors of Ruby did?
alex

--
Alex Polite
http://flo... - finding the right open source


Jules

1/28/2006 9:42:00 PM

0

I think 0 is false because of the way it is represented in low-level
languages: a 0 (in binary). False is 0 too, and true is 1.

The Ruby definition of false/true is much simpler: everything except nil
or false is true.

Jules

--
Posted via http://www.ruby-....


MenTaLguY

1/28/2006 9:58:00 PM

0

On Sun, 2006-01-29 at 06:39 +0900, Alex Polite wrote:
> Well I do understand that it's a convention. They same is true for a
> lot of things. Like the positioning of gas and break pedals in cars.
> It's just a convention. But when GM puts a new car in the market they
> don't swap them. My question is why the constructors of Ruby did?
> alex

Among other things, it makes things like ||= much more useful.

-mental



Eric Jacoboni

1/28/2006 10:02:00 PM

0

Alex Polite <notmyprivateemail@gmail.com> writes:

>> 0 *the integer* is only false by convention,
>
> Well I do understand that it's a convention.

Actually, that's *only* a C-like convention... 0 on Unix could be
considered as True, meaning a program execution succeed.

--
Eric Jacoboni, ne il y a 1441926044 secondes

Sebastian Steinlechner

1/28/2006 10:04:00 PM

0

Alex Polite wrote:
> Well I do understand that it's a convention. They same is true for a
> lot of things. Like the positioning of gas and break pedals in cars.
> It's just a convention. But when GM puts a new car in the market they
> don't swap them. My question is why the constructors of Ruby did?

Well... 0 evaluating to false is a convention used mainly in the C/C++
programming world. Many other languages that have been around for years
don't treat 0 as false. Pascal being one of them, IIRC. So Ruby is not
really breaking with a convention -- as there is none.

Sebastian
--
Drawing on my fine command of language, I said nothing.

Matthew Smillie

1/28/2006 10:32:00 PM

0


>> 0 *the integer* is only false by convention,
>
> Well I do understand that it's a convention. They same is true for a
> lot of things. Like the positioning of gas and break pedals in cars.

Computer languages are not cars, nor are all conventions equal. For
example, I'm unaware of any convention on what material the seats
should be covered in. As far as 0 goes, there are languages out
there which don't even use numbers, let alone define particular
integers as 'true' or 'false'.

There are any number of reasons to favour either approach over the
other, so I couldn't possibly pick the exact reasons Ruby is the way
that it is, but there are a couple reasons to not let 0 equate to
false if you look at the very top of this page:

http://www.rubygarden.org/ruby?...

matthew smillie.



Stefan Walk

1/28/2006 10:47:00 PM

0

Alex Polite wrote:
> Well I do understand that it's a convention. They same is true for a
> lot of things. Like the positioning of gas and break pedals in cars.
> It's just a convention. But when GM puts a new car in the market they
> don't swap them. My question is why the constructors of Ruby did?
> alex

When you get used to it, it's quite handy. Some methods return integers,
and false values on failures. If 0 was false, you'd have to distinguish
between 0 and false/nil when checking for success.

Regards,
Stefan