Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > types? casting and bools

Reply
Thread Tools

types? casting and bools

 
 
KONTRA Gergely
Guest
Posts: n/a
 
      12-12-2003
Hi!

Recently I ran into the problem:

Why ruby handle booleans so liberally?
I mean why can I pass anything to an if, and for constructs requiring
bool? I guess many programmers do it the wrong way saying "if 0" and
expecting to be true...

And on the other hand I must use to_i and others to convert from one
type to another.

Gergo
--
+-[ Kontra, Gergely<(E-Mail Removed)> PhD student Room IB113 ]---------+
| http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
| Mobil+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+

 
Reply With Quote
 
 
 
 
KONTRA Gergely
Guest
Posts: n/a
 
      12-13-2003
On 1213, David A. Black wrote:
> > Why ruby handle booleans so liberally?
> > I mean why can I pass anything to an if, and for constructs requiring
> > bool? I guess many programmers do it the wrong way saying "if 0" and
> > expecting to be true...

>
> It is true:


I meant false, of course...

> > And on the other hand I must use to_i and others to convert from one
> > type to another.

> You mean you want to have to put "to_bool" everywhere?


Not everywhere, just where needed...
Where should you put these? (examples)

Gergo

--
+-[ Kontra, Gergely<(E-Mail Removed)> PhD student Room IB113 ]---------+
| http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
| Mobil+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+


 
Reply With Quote
 
 
 
 
KONTRA Gergely
Guest
Posts: n/a
 
      12-13-2003
On 1213, Austin Ziegler wrote:
> On Sat, 13 Dec 2003 01:56:18 +0900, KONTRA Gergely wrote:
> > Why ruby handle booleans so liberally?

> It doesn't. Ruby treats all values excepting false and nil as true. It
> sounds like you're wanting 0 to be treated as false.

No, I'm just asking how ruby "dare" to convert my 0 to true (or false, it
is the same).
Everyone can write his/her own to_bool method INLINE. Shouldn't be a big
deal, but it can avoid some gotchas...

So in my philosophy, if 0 should raise a typeerror

So you are forced to write if 0!=0

(of course a=0;if a -> a=0; if a!=0 )

Do you think it's the camel's face?

Gergo

--
+-[ Kontra, Gergely<(E-Mail Removed)> PhD student Room IB113 ]---------+
| http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
| Mobil+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+

 
Reply With Quote
 
Wesley J Landaker
Guest
Posts: n/a
 
      12-13-2003
--Boundary-02=_mYz2/ItsBsWPhUS
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Description: signed data
Content-Disposition: inline

On Saturday 13 December 2003 8:37 am, KONTRA Gergely wrote:
> On 1213, Austin Ziegler wrote:
> > On Sat, 13 Dec 2003 01:56:18 +0900, KONTRA Gergely wrote:
> > > Why ruby handle booleans so liberally?

> >
> > It doesn't. Ruby treats all values excepting false and nil as true.
> > It sounds like you're wanting 0 to be treated as false.

>
> No, I'm just asking how ruby "dare" to convert my 0 to true (or
> false, it is the same).


Outside of any programming language, an abstract boolean has only two=20
values: true, and false.=20

Many programming languages map true to non-zero and false to zero, but=20
that isn't necessary a given. Return values for commands in a shell are=20
the opposite, 0 is true, anything non-zero is false. Even in languages=20
like C, technically 0 means true and non-zero means false, but it's=20
semantically exactly the opposite for 99.9% of all standard functions,=20
which return 0 for true, and non-zero for false.

Some languages, like Python or Perl, have a big list of what is false,=20
and everything else is true. Sometimes it's hard to know what is going=20
to be true or false in those languages unless you are very familiar=20
with the nuances.

In digital logic design, often 0 means false, and 1 means true. Except=20
if you're talking about shared bus architectures, then almost always 0=20
means true, 0 means false.

While it works for certain domains, it's probably not a good idea in=20
general to assume that integers map have any intrinsic mapping to=20
booleans: it's always a characteristic of the language or problem=20
domain in question.

In ruby, it's very simple and straightforward. Only two things are=20
logically false: nil, and false. There isn't any "conversion" going on,=20
it's an intrinsic part of the language. There is no "Boolean" data type=20
that things are being "converted" to; *everything* is true, *except*=20
nil and false.=20

=2D-=20
Wesley J. Landaker - http://www.velocityreviews.com/forums/(E-Mail Removed)
OpenPGP FP: 4135 2A3B 4726 ACC5 9094 0097 F0A9 8A4C 4CD6 E3D2


--Boundary-02=_mYz2/ItsBsWPhUS
Content-Type: application/pgp-signature
Content-Description: signature

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

iD8DBQA/2zYm8KmKTEzW49IRAtBYAJwMvW0or5Rx55WRq+U7BmSNPHRkTw CfWtM2
EB+5QK32/TT9jP/t3v2lab4=
=zJ1E
-----END PGP SIGNATURE-----

--Boundary-02=_mYz2/ItsBsWPhUS--

 
Reply With Quote
 
KONTRA Gergely
Guest
Posts: n/a
 
      12-13-2003
On 1214, Wesley J Landaker wrote:
Content-Description: signed data
> On Saturday 13 December 2003 8:37 am, KONTRA Gergely wrote:
> > On 1213, Austin Ziegler wrote:
> > > On Sat, 13 Dec 2003 01:56:18 +0900, KONTRA Gergely wrote:
> > > > Why ruby handle booleans so liberally?
> > > It doesn't. Ruby treats all values excepting false and nil as true.
> > > It sounds like you're wanting 0 to be treated as false.

> > No, I'm just asking how ruby "dare" to convert my 0 to true (or
> > false, it is the same).

> In ruby, it's very simple and straightforward. Only two things are
> logically false: nil, and false. There isn't any "conversion" going on,
> it's an intrinsic part of the language. There is no "Boolean" data type
> that things are being "converted" to; *everything* is true, *except*
> nil and false.

This is simple, but I think you must consider the above not to allow
everything to be boolean.
The time is NOW to change things (before 2.0)...

So in Rite will it be the same, or will the simplest class (boolean)
finally have its own class?

Gergo

--
+-[ Kontra, Gergely<(E-Mail Removed)> PhD student Room IB113 ]---------+
| http://www.mcl.hu/~kgergely "Olyan langesz vagyok, hogy |
| Mobil+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom" |
+-- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+


 
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
bcb strings and bools petelomax General Computer Support 0 07-21-2010 01:44 PM
Comparing two vectors of bools fgh.vbn.rty@gmail.com C++ 15 08-03-2008 03:09 PM
Linker error LNK2001 with bools Mike C++ 3 10-12-2004 11:46 AM
An array of bools ccs C++ 5 06-13-2004 03:05 AM
Python style of accessing bools? Jonathon McKitrick Python 3 04-15-2004 02:15 PM



Advertisments