# Evaluation in boolean context

**URL:** <https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635>\
**Category:** Ruby\
**Created:** [October 10, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635 "2006-10-10T14:52:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jens\_wille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jens\_wille](https://www.ruby-forum.com/u/jens_wille)\
**Post date:** [October 10, 2006, 2:52pm UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635/1 "2006-10-10T14:52:41Z")

</div>

hi there!

would it be possible to modify how an object gets evaluated in  
boolean context? i know that by default only nil and false are  
evaluated as false, all others are true. however, i’d like to change  
that behaviour for a (yet unimplemented) BooleanFlag object which  
behaves just like true or false (according to its current value or  
“state”) with the added ability of easily swapping state.

it’s not that difficult to implement a class that provides the state  
swapping, but how to grasp that “evaluation in boolean context” kind  
of thing? is there any method getting called, so i can override it?

the idea is as follows:

b = BooleanFlag.new false # create object, initially being “false”  
puts “true” if b # this is the heavy part, since b (as a  
# regular object) is always “true”  
b.swap! # now b’s state is “true” …  
puts “true” if b # … and this _correctly_ prints “true”

any insights? additionally, hints on how to address such issues on  
my own are very welcome. i’m quite new to ruby (coming from perl),  
but i definitely love it - especially the metaprogramming stuff 😉  
so maybe there’s a means of digging into such internals i’m not yet  
aware of.

TIA  
jens

---

<div class="post-metadata">

**Author:** ![jens\_wille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jens\_wille](https://www.ruby-forum.com/u/jens_wille)\
**Post date:** [October 10, 2006, 2:58pm UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635/2 "2006-10-10T14:58:48Z")

</div>

On 10/10/06, jens wille [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hi there!
> 
> would it be possible to modify how an object gets evaluated in  
> boolean context? i know that by default only nil and false are  
> evaluated as false, all others are true. however, i’d like to change  
> that behaviour for a (yet unimplemented) BooleanFlag object which  
> behaves just like true or false (according to its current value or  
> “state”) with the added ability of easily swapping state.

I’m not an expert myself, but couldn’t you use a normal variable which  
is assigned a value of either true or false?

b = false

puts “true” if b

b = !b

puts “true” if b

---

<div class="post-metadata">

**Author:** ![jens\_wille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jens\_wille](https://www.ruby-forum.com/u/jens_wille)\
**Post date:** [October 10, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635/3 "2006-10-10T15:02:48Z")

</div>

Bira [10/10/06 14:58]:

> I’m not an expert myself, but couldn’t you use a normal variable  
> which is assigned a value of either true or false?
> 
> b = false
> 
> puts “true” if b
> 
> b = !b  
> yeah, but that’s exactly what i want to avoid 😉

thanks anyway!  
jens

---

<div class="post-metadata">

**Author:** ![jens\_wille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jens\_wille](https://www.ruby-forum.com/u/jens_wille)\
**Post date:** [October 10, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635/4 "2006-10-10T15:10:50Z")

</div>

On 10.10.2006 14:50, jens wille wrote:

> swapping, but how to grasp that “evaluation in boolean context” kind  
> of thing? is there any method getting called, so i can override it?
> 
> the idea is as follows:
> 
> b = BooleanFlag.new false # create object, initially being “false”  
> puts “true” if b # this is the heavy part, since b (as a  
> # regular object) is always “true”  
> b.swap! # now b’s state is “true” …  
> puts “true” if b # … and this _correctly_ prints “true”

This has come up frequently in the past. The short story is that there  
is no way to do exactly that because of performance reasons. All sorts  
of things have been proposed including a method to\_b defined in Object  
as “return self” and which is automatically called in every boolean  
context.

> any insights? additionally, hints on how to address such issues on  
> my own are very welcome. i’m quite new to ruby (coming from perl),  
> but i definitely love it - especially the metaprogramming stuff 😉  
> so maybe there’s a means of digging into such internals i’m not yet  
> aware of.

You can still use the #to\_b pattern explicitly. This is probably the  
cleanest solution:

BooleanFlag = Struct.new(:flag) do  
def swap!() self.flag = !flag end  
alias :to\_b :flag  
end

> > f = BooleanFlag.new false  
> > =\> #
> 
> > f.to\_b  
> > =\> false
> 
> > f.swap!  
> > =\> true
> 
> > f.to\_b  
> > =\> true

Alternatively / additionally you can define methods on your flag class  
similar to what Smalltalk does:

BooleanFlag = Struct.new(:flag) do  
def swap!() self.flag = !flag end  
alias :to\_b :flag  
def iff() yield if to\_b; self end  
def els() yield unless to\_b; end  
end

> > f=BooleanFlag.new false  
> > =\> #
> 
> > f.iff do puts “yes” end.els do puts “no” end  
> > no  
> > =\> nil

Frankly, I never felt the need for any of these. Maybe you present your  
business case and we can propose some other solution.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![jens\_wille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jens\_wille](https://www.ruby-forum.com/u/jens_wille)\
**Post date:** [October 10, 2006, 10:39pm UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635/5 "2006-10-10T22:39:07Z")

</div>

On 10/10/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Alternatively / additionally you can define methods on your flag class  
> similar to what Smalltalk does:

Some observations on booleans in Ruby, Smalltalk, and self:

## [http://talklikeaduck.denhaven2.com/articles/2006/10/10/boolean-implementations-ruby-smalltak-and-self](http://talklikeaduck.denhaven2.com/articles/2006/10/10/boolean-implementations-ruby-smalltak-and-self)

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![jens\_wille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jens\_wille](https://www.ruby-forum.com/u/jens_wille)\
**Post date:** [October 10, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635/6 "2006-10-10T15:32:55Z")

</div>

hi robert!

Robert K. [10/10/06 15:10]:

> This has come up frequently in the past. The short story is that  
> there is no way to do exactly that because of performance  
> reasons. All sorts of things have been proposed including a  
> method to\_b defined in Object as “return self” and which is  
> automatically called in every boolean context.  
> i’ve read some of these postings, but felt they didn’t add much to  
> what i wanted to achieve.

> You can still use the #to\_b pattern explicitly. This is probably  
> the cleanest solution:  
> yes, it probably is. however, i had kind of hoped that there would  
> be a nice solution that fit more in my usage example. maybe i just  
> have to drop that 😉 thanks for your suggestions!

> Frankly, I never felt the need for any of these. Maybe you  
> present your business case and we can propose some other  
> solution.  
> well, actually i don’t have the need either. i tried to do it just  
> for the fun of it - to explore ruby’s possibilites and play around a  
> bit. i mean, as long as it can be achieved in another way there  
> simply is no need for any more “sophisticated” solution, is it? what  
> kind of bugged me was that “flag = !flag” part, but obviously i just  
> have to stick with it…

cheers  
jens

---

<div class="post-metadata">

**Author:** ![jens\_wille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/85f322/32.png) [@jens\_wille](https://www.ruby-forum.com/u/jens_wille)\
**Post date:** [October 11, 2006, 5:21am UTC](https://www.ruby-forum.com/t/evaluation-in-boolean-context/71635/7 "2006-10-11T05:21:36Z")

</div>

Rick DeNatale wrote:

> On 10/10/06, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Alternatively / additionally you can define methods on your flag class  
> > similar to what Smalltalk does:
> 
> Some observations on booleans in Ruby, Smalltalk, and self:
> 
> [http://talklikeaduck.denhaven2.com/articles/2006/10/10/boolean-implementations-ruby-smalltak-and-self](http://talklikeaduck.denhaven2.com/articles/2006/10/10/boolean-implementations-ruby-smalltak-and-self)

Rick, thanks for the interesting read! My gut feeling told me to better  
use “similar” and not “like” or “exactly”. :-)))

Kind regards

```
robert

```
