# Ruby way to define binary

**URL:** <https://rubytalk.org/t/ruby-way-to-define-binary/64295>\
**Category:** ruby-talk\
**Created:** [6 December 2011 20:05 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295 "2011-12-06T20:05:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Misha\_Ognev](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@Misha\_Ognev](https://rubytalk.org/u/Misha_Ognev)\
**Post date:** [6 December 2011 20:05 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295/1 "2011-12-06T20:05:13Z")

</div>

hi2all

I have some trouble. How can i define the binary of value in ruby? (I  
have a value, and I want to know that's it true or false).

The simplest (but not good in my opinion) way is to do it:

> v = value.to\_s  
> return v == "false" | v == "true"

What method I can use? I can't find something helpful in core.

Thanks, Michael

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Wayne\_Brisette](https://avatars.discourse-cdn.com/v4/letter/w/91b2a8/32.png) [@Wayne\_Brisette](https://rubytalk.org/u/Wayne_Brisette)\
**Post date:** [6 December 2011 20:42 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295/2 "2011-12-06T20:42:00Z")

</div>

> I have some trouble. How can i define the binary of value in ruby?  
> (I have a value, and I want to know that's it true or false).

Use match

ruby-1.9.2-p290 :001 \> value = "road"  
=\> "road"  
ruby-1.9.2-p290 :002 \> if value.match('road')  
ruby-1.9.2-p290 :003?\> puts "true"  
ruby-1.9.2-p290 :004?\> else  
ruby-1.9.2-p290 :005 \> puts "false"  
ruby-1.9.2-p290 :006?\> end  
true  
=\> nil  
ruby-1.9.2-p290 :007 \>

Wayne

---

<div class="post-metadata">

**Author:** ![Yong\_Li](https://avatars.discourse-cdn.com/v4/letter/y/ea666f/32.png) [@Yong\_Li](https://rubytalk.org/u/Yong_Li)\
**Post date:** [7 December 2011 02:16 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295/3 "2011-12-07T02:16:20Z")

</div>

not sure I understand your question correctly, but Ruby does not offer  
boolean data type as in many other programming languages. However,  
there are "true" and "false" objects which you can use for that  
purpose:  
v = true # or v = false  
if v then  
&nbsp;&nbsp;puts "v is true"  
else  
&nbsp;&nbsp;puts "v is false"  
end

> **···**
>
> On Wed, Dec 7, 2011 at 4:05 AM, Misha Ognev \<b1368810@lhsdv.com\> wrote:
> 
> > hi2all
> > 
> > I have some trouble. How can i define the binary of value in ruby? (I  
> > have a value, and I want to know that's it true or false).
> > 
> > The simplest (but not good in my opinion) way is to do it:
> > 
> > > v = value.to\_s  
> > > return v == "false" | v == "true"
> > 
> > What method I can use? I can't find something helpful in core.
> > 
> > Thanks, Michael
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marc\_Heiler](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@Marc\_Heiler](https://rubytalk.org/u/Marc_Heiler)\
**Post date:** [7 December 2011 08:56 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295/4 "2011-12-07T08:56:29Z")

</div>

I myself patched class String and added a .to\_bool method that turns  
"false" into false and "true" into true.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [7 December 2011 11:46 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295/5 "2011-12-07T11:46:27Z")

</div>

You don't really need to. Everything in ruby has a "truthiness" to it and can be used in logical expressions. Only nil and false are "falsey" and everything else are "truthy". In other words, just use objects and they'll take care of you.

if var then  
&nbsp;&nbsp;do\_something  
end

If you feel REALLY insecure about typing in a dynamic language you can always double-negate:

class Object  
&nbsp;&nbsp;def truthy?  
&nbsp;&nbsp;&nbsp;&nbsp;!!self  
&nbsp;&nbsp;end  
end

if var.truthy? then # wasted cycles for no gain  
&nbsp;&nbsp;do\_something  
end

> **···**
>
> On Dec 6, 2011, at 12:05 , Misha Ognev wrote:
> 
> > hi2all
> > 
> > I have some trouble. How can i define the binary of value in ruby? (I  
> > have a value, and I want to know that's it true or false).

---

<div class="post-metadata">

**Author:** ![Misha\_Ognev](https://avatars.discourse-cdn.com/v4/letter/m/b9e5f3/32.png) [@Misha\_Ognev](https://rubytalk.org/u/Misha_Ognev)\
**Post date:** [7 December 2011 11:50 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295/6 "2011-12-07T11:50:46Z")

</div>

> not sure I understand your question correctly, but Ruby does not offer  
> boolean data type as in many other programming languages.

I want to hear this 😉 Thanks!

> I myself patched class String and added a .to\_bool method that turns  
> "false" into false and "true" into true.

Why ruby core team don't want to add it to ruby lang?

Cheers, Michael

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Kaspar\_Schiess](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kaspar\_Schiess](https://rubytalk.org/u/Kaspar_Schiess)\
**Post date:** [7 December 2011 09:15 UTC](https://rubytalk.org/t/ruby-way-to-define-binary/64295/7 "2011-12-07T09:15:11Z")

</div>

Why would you ever need to represent a false as the string 'false'?

k

> **···**
>
> On 07.12.11 09:56, Marc Heiler wrote:
> 
> > I myself patched class String and added a .to\_bool method that turns  
> > "false" into false and "true" into true.
