# Make a class evaluate to false in boolean expressions

**URL:** <https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774>\
**Category:** Ruby\
**Created:** [October 9, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774 "2007-10-09T17:54:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/1 "2007-10-09T17:54:15Z")

</div>

I want to have a class that always evaluates to false in boolean  
expressions. Is there a way to extend Ruby so that it evaluates an  
object of a class to false like false and nil?

Example

class AlwaysFalse  
end

should\_be\_false = AlwaysFalse.new

if should\_be\_false  
#I don’t want this to ever run  
else  
#will always run  
end

The use case for this is to have the concept of false but have it  
contain instance variables. You can’t stick instance variables on  
FalseClass because it is a singleton and every use of false will  
overwrite the previous instance variable values.  
a = false  
a.name = “fish”  
b = false  
b.name = “ocean”  
#at this point in time a.name is equal to “ocean” and not “fish” like I  
want.

This example may seem like an awful design decision but in the program I  
am writing it is important to have an interface like this to meet the  
requirements I have to code against.

Is achieving this behavior as simple as redefining some operator methods  
like == or is this sort of behavior not possible in Ruby?

Thank you,  
Matt M.

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/2 "2007-10-09T18:01:53Z")

</div>

On 10/9/07, Matt M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a = false  
> Is achieving this behavior as simple as redefining some operator methods  
> like == or is this sort of behavior not possible in Ruby?

The latter.

OTOH:

class Object  
def false?  
false  
end  
end

class FalseClass  
def false?  
true  
end  
end

class YourClass  
def false?  
true  
end  
end

if condition.false?  
…  
end

Kind of annoying if you forget the .false? though. A better thing to  
do would be to figure out what you are trying to model by having  
multiple “falsy” values with some state.

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/3 "2007-10-09T18:08:20Z")

</div>

Logan C. wrote:

> > FalseClass because it is a singleton and every use of false will  
> > requirements I have to code against.  
> > def false?  
> > class YourClass  
> > do would be to figure out what you are trying to model by having  
> > multiple “falsy” values with some state.
> 
> > Thank you,  
> > Matt M.

I don’t want to have to call .false? though. Ideally what I would like  
to be able to do is just use the object in boolean expressions like  
false. Anyone have any idea if this is at all possible?

Matt M.

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 6:14pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/4 "2007-10-09T18:14:13Z")

</div>

On 10/9/07, Matt M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Logan C. wrote:
> 
> > On 10/9/07, Matt M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Is achieving this behavior as simple as redefining some operator methods  
> > > like == or is this sort of behavior not possible in Ruby?
> > 
> > The latter.

Quoting myself. In other words, no you can’t do it.

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 6:56pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/5 "2007-10-09T18:56:32Z")

</div>

On 09.10.2007 18:12, Logan C. wrote:

> Quoting myself. In other words, no you can’t do it.  
> An alternative approach might be to use ‘case’ expressions instead of  
> ‘if’ and implement #=== properly in that class.

Matt, what are you implementing? Why is this functionality crucial?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 7:48pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/6 "2007-10-09T19:48:52Z")

</div>

Robert K. wrote:

> > > >
> 
> ```
> robert
> 
> ```

I can’t really get into specifics but basically I am doing something  
like the following

module PrimitiveExtensions  
attr\_accessor :name  
def to\_extended\_primitive(name)  
@name = name  
return self  
end

I then mix this in to String, Fixnum, Float etc…

MyClass contains  
def my\_val=(obj)  
obj.to\_extended\_primitive(“MyVal”)  
end  
def other\_val=(obj)  
obj.to\_extended\_primitive(“OtherVal”)  
end

The purpose of all of this is to unify an interface so that I can store  
primitive values along with a name without having to go through a  
container class to get at the value.  
a = MyClass.new.my\_val = 7  
and then later be able to go back and do something like  
a.name and get back “MyVal”

This approach works for all the base types except for true and false  
since they are Singletons so I can’t store a @name on them.

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 7:34pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/7 "2007-10-09T19:34:07Z")

</div>

On Oct 9, 2007, at 10:53 AM, Matt M. wrote:

> if should\_be\_false  
> #I don’t want this to ever run  
> else  
> #will always run  
> end  
> This is your design problem.  
> You can’t change the language to do what it doesn’t do.  
> What you want is probably not a class if it always returns false.  
> You want a constant value.  
> You can create a class with an instance or class variable that  
> happens to be a constant. (no setter, only a getter)  
> so you might do

The problem is your if will go to else if the should\_be\_false is  
false. not clear logic in the code.  
Just do it the Ruby way, create a method or function:  
my\_class.false?  
or  
false?( my\_arg )

This way the logic is more obvious and clear like natural language.

---

<div class="post-metadata">

**Author:** ![mattmcswain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@mattmcswain](https://www.ruby-forum.com/u/mattmcswain)\
**Post date:** [October 9, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/make-a-class-evaluate-to-false-in-boolean-expressions/116774/8 "2007-10-09T23:20:32Z")

</div>

On 09.10.2007 19:41, Matt M. wrote:

> > > > > The latter.
> 
> end  
> end  
> This seems a strange way to use assignment since you do not modify the  
> receiver but the sender. I’d say this will likely lead to hard to code  
> that is hard to understand.

> The purpose of all of this is to unify an interface so that I can store  
> primitive values along with a name without having to go through a  
> container class to get at the value.  
> a = MyClass.new.my\_val = 7  
> and then later be able to go back and do something like  
> a.name and get back “MyVal”
> 
> This approach works for all the base types except for true and false  
> since they are Singletons so I can’t store a @name on them.

I doubt this approach works for all basic types since you will get  
aliasing effects with Fixnums, i.e. you set the name in one place and it  
will be visible on all other places as well as Fixnums are singletons  
per value.

I do not know why you are so eager to have the attribute stored in the  
primitive value. Considering what I have seen of your requirements I  
would probably do something like this

NamedValue = Struct.new :value, :name

Kind regards

robert
