# boolean to int conversion - Ruby

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

1. ## 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

Cere Guest

2. ## Re: boolean to int conversion

A few different ways:

1)
v = value ? 1 : 0

where value is either true or false
2)

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

Evan Webb // net

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

Evan Guest

3. ## Re: boolean to int conversion

Cere Davis wrote:

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
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"'

Jamis Guest

4. ## Re: boolean to int conversion

On Tue, 17 Feb 2004, Cere Davis wrote:

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

-a
--
================================================== =============================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do \$l -e "print \"\x3a\x2d\x29\x0a\"";done
================================================== =============================

Ara.T.Howard Guest

5. ## Re: boolean to int conversion

"Evan Webb" <net> schrieb im Newsbeitrag
news:..

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

[/ref]
go to 1 and False goto 0?
>
>
>[/ref]

Robert Guest

6. ## Re: boolean to int conversion

Thanks everyone for help on this...

Ara.T.Howard wrote:
>
>
> [true, false].each |bool| do
> int = (bool ? 1 : 0)
> p int
> end
>
> -a[/ref]
Cere Guest

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•