# Converting strings to booleans, symbols, etc

**URL:** <https://www.ruby-forum.com/t/converting-strings-to-booleans-symbols-etc/128551>\
**Category:** Ruby\
**Created:** [February 8, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/converting-strings-to-booleans-symbols-etc/128551 "2008-02-08T19:51:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 7:51pm UTC](https://www.ruby-forum.com/t/converting-strings-to-booleans-symbols-etc/128551/1 "2008-02-08T19:51:49Z")

</div>

Hi,

I am parsing an xml document, and I wanted to know if there is a quick  
built in way to convert a string to a symbol or a boolean. an example  
xml doc would be:

data

Now I know I can do some string compares, and set the variable  
accordingly, but I was wondering if there are shortcuts, much like the  
to\_i method.

thanks!

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/converting-strings-to-booleans-symbols-etc/128551/2 "2008-02-08T20:08:56Z")

</div>

On Feb 8, 2008 12:51 PM, an an [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am parsing an xml document, and I wanted to know if there is a quick  
> built in way to convert a string to a symbol or a boolean.

The symbol is easy, anyway:  
[http://www.ruby-doc.org/core/classes/String.html#M000825](http://www.ruby-doc.org/core/classes/String.html#M000825)

Once you’re there, making a compare for true/false isn’t so much code.  
But maybe someone else has a cleaner solution to that.

Ben

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/converting-strings-to-booleans-symbols-etc/128551/3 "2008-02-08T20:17:59Z")

</div>

Day wrote:

> On Feb 8, 2008 12:51 PM, an an [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I am parsing an xml document, and I wanted to know if there is a quick  
> > built in way to convert a string to a symbol or a boolean.
> 
> The symbol is easy, anyway:  
> [class String - RDoc Documentation](http://www.ruby-doc.org/core/classes/String.html#M000825)
> 
> Once you’re there, making a compare for true/false isn’t so much code.  
> But maybe someone else has a cleaner solution to that.
> 
> Ben

thanks…thats the type of stuff i was looking for…im used to java,  
and I am actively tried to avoid coding Java style in ruby, but  
sometimes its hard to figure out the shortcuts that ruby provides

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 10:53pm UTC](https://www.ruby-forum.com/t/converting-strings-to-booleans-symbols-etc/128551/4 "2008-02-08T22:53:57Z")

</div>

On Feb 8, 2008, at 11:51 AM, an an wrote:

> to\_i method.

Here some sample code to get your brain churning …

class String

# The inverse of ‘to\_s’

def s\_ot  
begin  
return Integer(self)  
rescue ArgumentError  
(return Float(self)) rescue nil  
end

```
 case self
 when %r/^true|yes$/i; true
 when %r/^false|no$/i; false
 when %r/^nil|none$/i; nil
 when %r/^:\w+$/; self.tr(':','').to_sym
 else self end

```

end  
end

“1.0”.s\_ot #=\> 1.0 (as a Float)  
“2”.s\_ot #=\> 2 (as a Fixnum)  
“no”.s\_ot #=\> false  
“none”.s\_ot #=\> nil  
“:string”.s\_ot #=\> :string (as a Symbol)

Happy Friday everyone!

Blessings,  
TwP

---

<div class="post-metadata">

**Author:** ![gmoniey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/6a8cbe/32.png) [@gmoniey](https://www.ruby-forum.com/u/gmoniey)\
**Post date:** [February 8, 2008, 11:05pm UTC](https://www.ruby-forum.com/t/converting-strings-to-booleans-symbols-etc/128551/5 "2008-02-08T23:05:46Z")

</div>

On 08.02.2008 19:51, an an wrote:

> to\_i method.  
> You can create yourself some. One example:

CONV = {  
“false” =\> false,  
“true” =\> true,  
“0” =\> 0,  
“1” =\> 1,

# more common / important values

}

def convert(s)  
x = CONV[s]

if x.nil?  
x = case x  
when /\A[±]?\d+\z/  
s.to\_i  
when /\A[±]?\d+(?:.\d+)?\z/  
s.to\_f  
# more  
else  
x  
end  
end

x  
end

Kind regards

robert
