Boolean to int conversion

I’m a bit of a Ruby newbie so be kind pls.

Could someone tell me if there’s an easy way to make a value of True go to 1 and False goto 0?

a sort of to_i method?

Thanks,
Cere

A few different ways:

  1. v = value ? 1 : 0

    where value is either true or false

class TrueClass; def to_i; 1; end; end
class FalseClass; def to_i; 0; end; end

Evan Webb // evan@fallingsnow.net

···

On Tue, 2004-02-17 at 19:09, Cere Davis wrote:

I’m a bit of a Ruby newbie so be kind pls.

Could someone tell me if there’s an easy way to make a value of True go to 1 and False goto 0?

a sort of to_i method?

Thanks,
Cere

Cere Davis wrote:

I’m a bit of a Ruby newbie so be kind pls.

Could someone tell me if there’s an easy way to make a value of True
go to 1 and False goto 0?

a sort of to_i method?

Thanks,
Cere

Besides adding them yourself, I don’t think so. However, you can use
the ternary operator to good effect here:

a = true
puts( a ? 1 : 0 )
a = false
puts( a ? 1 : 0 )

···


Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/blog/jamis.cgi

ruby -h | ruby -e ‘a=;readlines.join.scan(/-(.)[e|Kk(\S*)|le.l(…)e|#!(\S*)/) {|r| a << r.compact.first };puts “\n>#{a.join(%q/ /)}<\n\n”’

bar = foo ? 1 : 0

Now the real question: why do you want it to do so?

-austin

···

On Wed, 18 Feb 2004 12:09:54 +0900, Cere Davis wrote:

I’m a bit of a Ruby newbie so be kind pls.

Could someone tell me if there’s an easy way to make a value of True go
to 1 and False goto 0?

a sort of to_i method?


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2004.02.17
* 22.37.32

[true, false].each |bool| do
int = (bool ? 1 : 0)
p int
end

-a

···

On Tue, 17 Feb 2004, Cere Davis wrote:

Date: Tue, 17 Feb 2004 19:05:59 -0800
From: Cere Davis cere@u.washington.edu
Newsgroups: comp.lang.ruby
Subject: boolean to int conversion

I’m a bit of a Ruby newbie so be kind pls.

Could someone tell me if there’s an easy way to make a value of True go to 1 and False goto 0?

a sort of to_i method?

Thanks,
Cere

EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
PHONE :: 303.497.6469
ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
URL :: Solar-Terrestrial Physics Data | NCEI
TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done
===============================================================================

“Evan Webb” evan@fallingsnow.net schrieb im Newsbeitrag
news:1077074531.9643.4.camel@maxwell…

A few different ways:

  1. v = value ? 1 : 0

    where value is either true or false

In fact “value” might be anything with “false” and “nil” meaning “false”
(and evaluating to “0” in this case) and everything else meaning “true”
(yields “1”).

Regards

robert

class TrueClass; def to_i; 1; end; end
class FalseClass; def to_i; 0; end; end

Evan Webb // evan@fallingsnow.net

I’m a bit of a Ruby newbie so be kind pls.

Could someone tell me if there’s an easy way to make a value of True
go to 1 and False goto 0?

···

On Tue, 2004-02-17 at 19:09, Cere Davis wrote:

a sort of to_i method?

Thanks,
Cere

Thanks everyone for help on this…

Ara.T.Howard wrote:

···

On Tue, 17 Feb 2004, Cere Davis wrote:

Date: Tue, 17 Feb 2004 19:05:59 -0800
From: Cere Davis cere@u.washington.edu
Newsgroups: comp.lang.ruby
Subject: boolean to int conversion

I’m a bit of a Ruby newbie so be kind pls.

Could someone tell me if there’s an easy way to make a value of True go to 1 and False goto 0?

a sort of to_i method?

Thanks,
Cere

[true, false].each |bool| do
int = (bool ? 1 : 0)
p int
end

-a