# Object#to\_b

**URL:** <https://www.ruby-forum.com/t/object-to-b/67427>\
**Category:** Ruby\
**Created:** [November 16, 2005, 10:37pm UTC](https://www.ruby-forum.com/t/object-to-b/67427 "2005-11-16T22:37:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [November 16, 2005, 10:37pm UTC](https://www.ruby-forum.com/t/object-to-b/67427/1 "2005-11-16T22:37:55Z")

</div>

Currently, as far as I know, `if' and`unless’ merely check if an object  
is of either types NilClass or FalseClass. This seems to contradict the  
duck-typing paradigm. I propose that we implement a method Object#to\_b  
that is called by `if' and`unless’. It should return a boolean value.  
That way, a developer can decide whether or not an object should  
evaluate to true or false.

class Connection  
def open?; end  
def closed?; end  
def to\_b; open?; end  
end

connection = Connection.new(…)  
if connection  
# …  
end

Implementation:

class Object  
def to\_b  
true  
end  
end

class FalseClass  
def to\_b  
false  
end  
end

class NilClass  
def to\_b  
false  
end  
end

One problem I see with this proposal is that it seems like a lot of  
people check if a variable has been set by writing `if var ...'. This change would require that people wrote`unless var.nil?’, which I  
personally find more correct as well.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [November 16, 2005, 10:56pm UTC](https://www.ruby-forum.com/t/object-to-b/67427/2 "2005-11-16T22:56:12Z")

</div>

On 11/16/05, Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Currently, as far as I know, `if' and `unless’ merely check if an object  
> is of either types NilClass or FalseClass. This seems to contradict the  
> duck-typing paradigm. I propose that we implement a method Object#to\_b  
> that is called by `if' and `unless’.It should return a boolean value.  
> That way, a developer can decide whether or not an object should  
> evaluate to true or false.

You could also ask that “and”, “or”, “not”, “!”, “||”, “&&” be  
operator methods. I think the reason all of this is built-in is  
performance. All of this stuff doesn’t have to go through the normal  
method-call mechanism. If you do a benchmark on “unless x” vs. “if  
x.nil?” you’ll see the difference.

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [November 16, 2005, 10:53pm UTC](https://www.ruby-forum.com/t/object-to-b/67427/3 "2005-11-16T22:53:11Z")

</div>

On 11/16/05, Daniel S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Currently, as far as I know, `if' and `unless’ merely check if an object  
> is of either types NilClass or FalseClass. This seems to contradict the  
> duck-typing paradigm. I propose that we implement a method Object#to\_b  
> that is called by `if' and `unless’. It should return a boolean value.  
> That way, a developer can decide whether or not an object should  
> evaluate to true or false.

> One problem I see with this proposal is that it seems like a lot of  
> people check if a variable has been set by writing `if var ...'. This change would require that people wrote `unless var.nil?', which I  
> personally find more correct as well.

This would break a lot of my code, personally. I used to use “unless  
var.nil?” but it is easier to say:

if var and var.foo

than:

if (not var.nil?) and var.foo

I’m not _quite_ sure what the “unless” version of the test would be:

unless var.nil? or var.foo.nil?

Not quite what I want. I think that #to\_b would be problematic,  
especially in the example that you gave, since it’s more expressive to  
say:

if conn.open?

than:

if conn

which implies you’re checking to see if conn is really there.

-austin

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [November 17, 2005, 3:50pm UTC](https://www.ruby-forum.com/t/object-to-b/67427/4 "2005-11-17T15:50:32Z")

</div>

Joel VanderWerf wrote:

> … or Object#not\_to\_b, that is the question.
> 
> I’m already sorry I said that 😉

class Foo  
def to\_b; end  
def bar  
if to\_b or not to\_b  
# …  
end  
end  
end

🙂

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [November 17, 2005, 5:15am UTC](https://www.ruby-forum.com/t/object-to-b/67427/5 "2005-11-17T05:15:45Z")

</div>

… or Object#not\_to\_b, that is the question.

I’m already sorry I said that 😉

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [November 17, 2005, 3:50pm UTC](https://www.ruby-forum.com/t/object-to-b/67427/6 "2005-11-17T15:50:32Z")

</div>

Austin Z. wrote:

> I used to use “unless  
> var.nil?” but it is easier to say:
> 
> if var and var.foo
> 
> than:
> 
> if (not var.nil?) and var.foo

`if var and var.foo' would still work. If`var’ is undefined (nil), its  
#to\_b method will return false. So unless var is an object that  
overrides the #to\_b method, your code will still work.

I can see the problem with the performance Matz brought up, but this was  
more meant as a philosophical discussion (though I can see now that I  
was quite concrete.) It actually came in response to the whole  
“subclassing FalseClass and NilClass” discussion, where I believe the  
#to\_b method would be more true to the idea of duck-typing.

Cheers,  
Daniel

---

<div class="post-metadata">

**Author:** ![dasch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@dasch](https://www.ruby-forum.com/u/dasch)\
**Post date:** [November 17, 2005, 6:09pm UTC](https://www.ruby-forum.com/t/object-to-b/67427/7 "2005-11-17T18:09:24Z")

</div>

Joel VanderWerf wrote:

> … or Object#not\_to\_b, that is the question.
> 
> I’m already sorry I said that 😉

With good reason 😉

class Object; def to\_b; !!self; end; end  
alias that? to\_b

that? #-\> true

daz
