# DRY? Converting Boolean to Yes/No

**URL:** <https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754>\
**Category:** Rails\
**Created:** [July 17, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754 "2006-07-17T23:37:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [July 17, 2006, 11:37pm UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754/1 "2006-07-17T23:37:19Z")

</div>

I have a database column given as:

t.column :change\_address, :boolean

The user interacts with this using a checkbox, but wants confirmations  
to  
read:

Change address: Yes

In an isolated case something like:

@obj.change\_address ? ‘Yes’ : ‘No’

Would work fine but this is peppered throughout scads of forms and  
boolean  
fields. The first step I took at DRYing this up was:

class TrueClass  
def to\_s  
“Yes”  
end  
end

class FalseClass  
def to\_s  
“No”  
end  
end

But this breaks other uses. For example observe\_field constructs the  
observers based on the to\_s conversion of boolean attributes.

Any ideas on how to DRY this usage up?

## Thanks

View this message in context:  
[http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5368812](http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5368812)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [July 17, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754/2 "2006-07-17T23:52:54Z")

</div>

You could try something like:  
def change\_address?  
self.change\_address ? “Yes” : “No”  
end

in the model.

Steve R. wrote:

> I have a database column given as:
> 
> t.column :change\_address, :boolean
> 
> The user interacts with this using a checkbox, but wants confirmations  
> to  
> read:
> 
> Change address: Yes
> 
> In an isolated case something like:
> 
> @obj.change\_address ? ‘Yes’ : ‘No’
> 
> Would work fine but this is peppered throughout scads of forms and  
> boolean  
> fields. The first step I took at DRYing this up was:
> 
> class TrueClass  
> def to\_s  
> “Yes”  
> end  
> end
> 
> class FalseClass  
> def to\_s  
> “No”  
> end  
> end
> 
> But this breaks other uses. For example observe\_field constructs the  
> observers based on the to\_s conversion of boolean attributes.
> 
> Any ideas on how to DRY this usage up?
> 
> ## Thanks
> 
> View this message in context:  
> [http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5368812](http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5368812)  
> Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [July 17, 2006, 11:58pm UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754/3 "2006-07-17T23:58:27Z")

</div>

I created a ‘prettifier’ plugin that extended TrueClass and FalseClass  
with a ‘prettier’ method that gave me a Yes/No response. More useful if  
you need the Yes/No to be available across the board. Yehuda’s response  
should be sufficient if you just need it for one column.

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [July 18, 2006, 12:02am UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754/4 "2006-07-18T00:02:19Z")

</div>

Yes, this handles the individual case in questions, but I have dozens of  
models and many of them have one or more boolean attributes. The larger  
question, then, is how to generalize the pattern?

Thanks

wycats wrote:

> You could try something like:  
> def change\_address?  
> self.change\_address ? “Yes” : “No”  
> end

–  
View this message in context:  
[http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369236](http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369236)  
Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [July 18, 2006, 12:08am UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754/5 "2006-07-18T00:08:59Z")

</div>

Where is the plugin or could you post relevant code?

Philip S. wrote:

> I created a ‘prettifier’ plugin that extended TrueClass and FalseClass  
> with a ‘prettier’ method that gave me a Yes/No response.  
> –  
> View this message in context:  
> [http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369356](http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369356)  
> Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [July 18, 2006, 12:08am UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754/6 "2006-07-18T00:08:31Z")

</div>

./script/generate plugin pretty\_boolean

And then modify/create the following two files:

# \<RAILS\_APP\>/vendor/plugins/pretty\_boolean/init.rb

# Include hook code here

require ‘pretty\_boolean’

# End init.rb

# \<RAILS\_APP\>/vendor/plugins/pretty\_boolean/lib/pretty\_boolean.rb

# PrettyBoolean

module PrettyBoolean  
def prettier  
self ? “yes” : “no”  
end  
end

class FalseClass  
include PrettyBoolean  
end

class TrueClass  
include PrettyBoolean  
end

# End PrettyBoolean

and, as long as the plugin is working, you can take any boolean you want  
and go  
some\_boolean.prettier

and it’ll return “yes” or “no”

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [July 18, 2006, 12:12am UTC](https://www.ruby-forum.com/t/dry-converting-boolean-to-yes-no/98754/7 "2006-07-18T00:12:43Z")

</div>

Steve R. wrote:

> Where is the plugin or could you post relevant code?
> 
> Philip S. wrote:
> 
> > I created a ‘prettifier’ plugin that extended TrueClass and FalseClass  
> > with a ‘prettier’ method that gave me a Yes/No response.  
> > –  
> > View this message in context:  
> > [http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369356](http://www.nabble.com/DRY--Converting-Boolean-to-Yes-No-tf1957326.html#a5369356)  
> > Sent from the RubyOnRails Users forum at [Nabble.com](http://Nabble.com).

I think that we’re posting in between eachother here… I just posted  
the relevant code 😃
