# Symbol to true object

**URL:** <https://www.ruby-forum.com/t/symbol-to-true-object/86827>\
**Category:** Ruby\
**Created:** [February 26, 2007, 8:19am UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827 "2007-02-26T08:19:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 26, 2007, 8:19am UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827/1 "2007-02-26T08:19:56Z")

</div>

Hi,  
Is there an idiom to convert the symbol :true to object true (of  
TrueClass)?

Basically how can I better

def validate\_the\_message (sym\_arg)  
@vtm =  
case sym\_arg  
when :true  
true  
when :false  
false  
else  
# do something else  
end  
end

(assume @vtm an instance variable holding the choice.)

TIA

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 26, 2007, 10:40am UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827/2 "2007-02-26T10:40:17Z")

</div>

On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir K. wrote:

> Is there an idiom to convert the symbol :true to object true (of TrueClass)?

BOOL\_MAP = { :true=\>true, :false=\>false }  
BOOL\_MAP[sym\_arg]

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 26, 2007, 11:05am UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827/3 "2007-02-26T11:05:19Z")

</div>

On Feb 26, 10:39 am, Brian C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mon, Feb 26, 2007 at 04:19:13PM +0900, Nasir K. wrote:
> 
> > Is there an idiom to convert the symbol :true to object true (of TrueClass)?
> 
> BOOL\_MAP = { :true=\>true, :false=\>false }  
> BOOL\_MAP[sym\_arg]

Or

class Symbol  
def to\_bool  
if to\_s.eql?(“true”)  
true  
elsif to\_s.eql?(“false”)  
false  
else  
raise ArgumentError  
end  
end  
end

:true.to\_bool =\> true  
:false.to\_bool =\> false  
:nothing.to\_bool =\> ArgumentError

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 26, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827/4 "2007-02-26T15:09:48Z")

</div>

On Mon, Feb 26, 2007 at 07:05:05PM +0900, Hans S. wrote:

> def to\_bool  
> if to\_s.eql?(“true”)  
> if eql?(:true)  
> true  
> elsif to\_s.eql?(“false”)  
> elsif eql?(:false)

There, fixed that for you 🙂

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 26, 2007, 3:21pm UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827/5 "2007-02-26T15:21:50Z")

</div>

On Feb 26, 3:08 pm, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > class Symbol  
> > end  
> > end
> 
> > :true.to\_bool =\> true  
> > :false.to\_bool =\> false  
> > :nothing.to\_bool =\> ArgumentError
> 
> There, fixed that for you 🙂
> 
> > –  
> > Hans

Ah oops, thank your for that 🙂

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 26, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827/6 "2007-02-26T16:58:07Z")

</div>

> > > ```
> > > else
> > > raise ArgumentError
> > > end
> > > 
> > > ```
> > > 
> > > end  
> > > end
> > 
> > > :true.to\_bool =\> true  
> > > :false.to\_bool =\> false  
> > > :nothing.to\_bool =\> ArgumentError

class Symbol  
def to\_bool  
case to\_s  
when “true” then true  
when “false” then false  
else raise ArgumentError  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Nasir\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/838e76/32.png) [@Nasir\_K](https://www.ruby-forum.com/u/Nasir_K)\
**Post date:** [February 26, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/symbol-to-true-object/86827/7 "2007-02-26T19:26:38Z")

</div>

On Tue, Feb 27, 2007 at 12:57:30AM +0900, Gareth A. wrote:

> class Symbol  
> def to\_bool  
> case to\_s  
> when “true” then true  
> when “false” then false  
> else raise ArgumentError  
> end  
> end  
> end

Why to\_s?

class Symbol  
def to\_bool  
case self  
when :true then true  
when :false then false  
else raise ArgumentError  
end  
end  
end
