# Boolean to int conversion

**URL:** <https://rubytalk.org/t/boolean-to-int-conversion/10211>\
**Category:** ruby-talk\
**Created:** [18 February 2004 03:15 UTC](https://rubytalk.org/t/boolean-to-int-conversion/10211 "2004-02-18T03:15:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mehr\_Assaph\_Assaph](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Mehr\_Assaph\_Assaph](https://rubytalk.org/u/Mehr_Assaph_Assaph)\
**Post date:** [18 February 2004 03:15 UTC](https://rubytalk.org/t/boolean-to-int-conversion/10211/1 "2004-02-18T03:15:34Z")

</div>

Ruby is dynamic, and you can always modify existing classes:

irb(main):004:0\> class TrueClass  
irb(main):005:1\> def to\_i  
irb(main):006:2\> 1  
irb(main):007:2\> end  
irb(main):008:1\> end  
nil  
irb(main):009:0\> class FalseClass  
irb(main):010:1\> def to\_i  
irb(main):011:2\> 0  
irb(main):012:2\> end  
irb(main):013:1\> end  
nil  
irb(main):014:0\> true.to\_i  
1  
irb(main):015:0\> false.to\_i  
0  
irb(main):016:0\> (1==1).to\_i  
1  
irb(main):017:0\> (1==2).to\_i  
0

Ruby considers false to be only nil (The singleton instance of NilClass)  
and false (the singleton instance of FalseClass). Everything else is  
true.  
The question remains, why do you need it? What can’t you do the ruby  
way?

HTH,  
Assaph

> **···**
>
> -----Original Message-----  
> From: Cere Davis [[mailto:cere@u.washington.edu](mailto:cere@u.washington.edu)]  
> Sent: Wednesday, 18 February 2004 14:10 PM  
> To: ruby-talk ML  
> 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
