nil != []

A

Alex Polite

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
 
V

Vince Puzzella

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

Simon Kröger

Alex said:
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

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
 
M

Matthew Smillie

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.
 
A

Alex Polite

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
 
J

Jules Jacobs

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
 
M

MenTaLguY

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
 
E

Eric Jacoboni

Alex Polite said:
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.
 
S

Sebastian Steinlechner

--------------enig39DDE9B1834A03A468DCCA3B
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

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

--------------enig39DDE9B1834A03A468DCCA3B
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFD2+pJ8X9UC2T3lakRAsCEAJ9KV6JV719MjGDAsgU1Kf3L1CJJlgCdHYZA
qSKY+IUg5t4oyzskofeJ6Ok=
=4czZ
-----END PGP SIGNATURE-----

--------------enig39DDE9B1834A03A468DCCA3B--
 
M

Matthew Smillie

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

matthew smillie.
 
S

Stefan Walk

Alex said:
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
 
A

Alex Polite

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.

yeah. that makes sense.

alex
 
C

Clint Checketts

------=_Part_11050_27563360.1138560705449
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline


Dynamic typing in Ruby also aids the use of false =3D false and 0 =3D 0.

In Java I can't suddenly say that my variable contains a 'False' value. I
would have to make my int =3D 0 and detect the rationale behind that specif=
ic
integer. Dynamic typing solves this because myVariable =3D 5 and later it c=
an
equal 'false' when it really is false (rather than a
number that represents false)

-Clint

------=_Part_11050_27563360.1138560705449--
 

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
474,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top