# How to "cast" in Ruby

**URL:** <https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616>\
**Category:** Ruby\
**Created:** [August 21, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616 "2007-08-21T13:59:46Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 1:59pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/1 "2007-08-21T13:59:46Z")

</div>

Hi,  
I need to know how to “cast” ruby objects on another i.e.

arr = Array.new()  
doc = REXML::Document.new(grListXML)  
doc.elements.each("\*/group") do |el|  
temp = el.elements[“id”].get\_text  
arr \<\< temp  
end

I want to convert temp variable to intager. Is is any simple way to do  
it if REXML has no to\_i methods?

Thanks for any help.  
MT

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/2 "2007-08-21T14:07:15Z")

</div>

Hi –

On Tue, 21 Aug 2007, Marcin T. wrote:

> I want to convert temp variable to intager. Is is any simple way to do  
> it if REXML has no to\_i methods?

If temp is a string, then it does have a to\_i method, and you can call  
that:

arr \<\< temp.to\_i

David

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 2:12pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/3 "2007-08-21T14:12:15Z")

</div>

> ## arr = Array.new() Thanks for any help. MT
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I’m not particularly familiar with REXML, but any string has a to\_i  
method:

felix@felix-laptop:~$ cat demo.xml  
  
  
1  
  
  
felix@felix-laptop:~$ irb  
irb(main):001:0\> require ‘rexml/document’  
=\> true  
irb(main):002:0\> xml = REXML::Document.new(File.open(‘demo.xml’))  
=\> … \</\>  
irb(main):003:0\> p xml.elements[“//class[@name=‘testclass’]”].text  
“\n 1\n "  
=\> nil  
irb(main):004:0\> p  
xml.elements[”//class[@name=‘testclass’]“].text.lstrip.rstrip  
“1”  
=\> nil  
irb(main):005:0\> p  
xml.elements[”//class[@name=‘testclass’]"].text.lstrip.rstrip.to\_i  
1  
=\> nil  
irb(main):006:0\>

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/4 "2007-08-21T14:26:16Z")

</div>

Hi –

On Tue, 21 Aug 2007, Marcin T. wrote:

> > arr \<\< temp.to\_i
> > 
> > David
> 
> David,  
> Your solution doesn’t work correctly. I’ve resolved this like that
> 
> temp = String.new("#{el.elements[“id”].get\_text}")  
> arr \<\< temp.to\_i

I think text (rather than get\_text) will give you a string. In any  
case, you don’t need that whole line; at the very least you can get  
rid of String.new 🙂

David

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/5 "2007-08-21T14:13:00Z")

</div>

David A. Black wrote:

> Hi –
> 
> On Tue, 21 Aug 2007, Marcin T. wrote:
> 
> > I want to convert temp variable to intager. Is is any simple way to do  
> > it if REXML has no to\_i methods?
> 
> If temp is a string, then it does have a to\_i method, and you can call  
> that:
> 
> arr \<\< temp.to\_i
> 
> David

David,  
Your solution doesn’t work correctly. I’ve resolved this like that

temp = String.new("#{el.elements[“id”].get\_text}")  
arr \<\< temp.to\_i

Thanks for directing!

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 3:16pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/6 "2007-08-21T15:16:24Z")

</div>

On Aug 21, 8:13 am, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> David,  
> Your solution doesn’t work correctly. I’ve resolved this like that
> 
> temp = String.new(“#{el.elements[“id”].get\_text}”)  
> arr \<\< temp.to\_i

How about: arr \<\< “#{el.elements[“id”].get\_text}”.to\_i ?

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 4:19pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/7 "2007-08-21T16:19:41Z")

</div>

2007/8/21, Kaldrenon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> On Aug 21, 8:13 am, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > David,  
> > Your solution doesn’t work correctly. I’ve resolved this like that
> > 
> > temp = String.new(“#{el.elements[“id”].get\_text}”)  
> > arr \<\< temp.to\_i
> 
> How about: arr \<\< “#{el.elements[“id”].get\_text}”.to\_i ?

Then I’d still prefer arr \<\< el.elements[“id”].get\_text.to\_s.to\_i

String interpolation for just one value doesn’t really make sense.

robert

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/8 "2007-08-21T17:20:57Z")

</div>

On Aug 21, 7:12 am, Kaldrenon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Aug 21, 8:13 am, Marcin T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > David,  
> > Your solution doesn’t work correctly. I’ve resolved this like that
> 
> > temp = String.new(“#{el.elements[“id”].get\_text}”)  
> > arr \<\< temp.to\_i
> 
> How about: arr \<\< “#{el.elements[“id”].get\_text}”.to\_i ?

If you ever are tempted to write:  
“#{foo}”  
you should realize that it’s functionally equivalent to  
foo.to\_s

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/9 "2007-08-21T19:38:27Z")

</div>

On Aug 21, 2007, at 10:19 AM, Phrogz wrote:

> If you ever are tempted to write:  
> “#{foo}”  
> you should realize that it’s functionally equivalent to  
> foo.to\_s

True, but for some (myself included) it definitely stands out more,  
being more readable. But usage depends on context.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 22, 2007, 4:12am UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/10 "2007-08-22T04:12:25Z")

</div>

On 8/21/07, Kaldrenon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It’s also exactly the same number of characters.

I had to count to be sure, but you are indeed correct. 🙂 I just think  
“#{foo}” looks busier than foo.to\_s

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 21, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/11 "2007-08-21T20:01:29Z")

</div>

On Aug 21, 11:15 am, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > How about: arr \<\< “#{el.elements[“id”].get\_text}”.to\_i ?
> 
> If you ever are tempted to write:  
> “#{foo}”  
> you should realize that it’s functionally equivalent to  
> foo.to\_s

It is. But “#{foo}” was more in keeping with the OP’s original code,  
and I personally prefer to keep the length of call chains (as in  
foo.bar.xxx.yyy.zzz) to a minimum. It ultimately amounts to  
preference, IMO, as both are readily legible and easy to understand.  
It’s also exactly the same number of characters.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 22, 2007, 2:31pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/12 "2007-08-22T14:31:13Z")

</div>

On Aug 21, 10:11 pm, “Logan C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 8/21/07, Kaldrenon [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > It’s also exactly the same number of characters.
> 
> I had to count to be sure, but you are indeed correct. 🙂 I just think  
> “#{foo}” looks busier than foo.to\_s

By itself, I happen to agree, but I tend to dislike long method call  
chains as in the original example. They both do the same thing, so  
it’s just a preference thing, I think.

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 22, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/13 "2007-08-22T14:52:14Z")

</div>

On Aug 22, 8:35 am, James Edward G. II [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > By itself, I happen to agree, but I tend to dislike long method call  
> > Benchmark.bmbm do |results|
> 
> # \>\> to\_s: 0.820000 0.000000 0.820000 ( 0.820689)
> 
> **END**

Definitely a good thing to keep in mind, thanks James. However, that  
only amounts to a difference of about .6 millionths of a second per  
evaluation, so I’m thinking that there are a number of cases where  
it’s negligible?

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 22, 2007, 2:37pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/14 "2007-08-22T14:37:13Z")

</div>

On Aug 22, 2007, at 7:29 AM, Kaldrenon wrote:

> it’s just a preference thing, I think.  
> Well, one definitely has Ruby doing more work:

#!/usr/bin/env ruby -wKU

require “benchmark”

TESTS = 1\_000\_000  
Benchmark.bmbm do |results|  
results.report(“iterpolation:”) { TESTS.times { “#{TESTS}” } }  
results.report(“to\_s:”) { TESTS.times { TESTS.to\_s } }  
end

# \>\> Rehearsal -------------------------------------------------

# \>\> iterpolation: 1.400000 0.000000 1.400000 ( 1.404947)

# \>\> to\_s: 0.820000 0.000000 0.820000 ( 0.817229)

# \>\> ---------------------------------------- total: 2.220000sec

# \>\>

# \>\> user system total real

# \>\> iterpolation: 1.400000 0.000000 1.400000 ( 1.395974)

# \>\> to\_s: 0.820000 0.000000 0.820000 ( 0.820689)

**END**

James Edward G. II

---

<div class="post-metadata">

**Author:** ![sirtyman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/6a8cbe/32.png) [@sirtyman](https://www.ruby-forum.com/u/sirtyman)\
**Post date:** [August 22, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/how-to-cast-in-ruby/112616/15 "2007-08-22T16:23:29Z")

</div>

On Aug 22, 2007, at 8:49 AM, Kaldrenon wrote:

> > > > “#{foo}” looks busier than foo.to\_s
> > 
> > # \>\> user system total real
> > 
> > # \>\> iterpolation: 1.400000 0.000000 1.400000 ( 1.395974)
> > 
> > # \>\> to\_s: 0.820000 0.000000 0.820000 ( 0.820689)
> > 
> > **END**
> 
> Definitely a good thing to keep in mind, thanks James. However, that  
> only amounts to a difference of about .6 millionths of a second per  
> evaluation, so I’m thinking that there are a number of cases where  
> it’s negligible?

Look at it this way, you’re doing (1.4/.82)-1 ~ .707 or 70.7% more  
work for this case. But what if the variable being interpolated is  
already a string?

#!/usr/bin/env ruby -wKU

require “benchmark”

TESTS = 1\_000\_000  
SUT = “This is a string under test”  
Benchmark.bmbm do |results|  
results.report(“iterpolation:”) { TESTS.times { “#{SUT}” } }  
results.report(“to\_s:”) { TESTS.times { SUT.to\_s } }  
end

**END**  
Rehearsal -------------------------------------------------  
iterpolation: 0.860000 0.000000 0.860000 ( 0.859359)  
to\_s: 0.290000 0.000000 0.290000 ( 0.294972)  
---------------------------------------- total: 1.150000sec

```
                 user system total real

```

iterpolation: 0.860000 0.000000 0.860000 ( 0.858763)  
to\_s: 0.310000 0.000000 0.310000 ( 0.303252)

This is (.86/.31)-1 ~ 1.774 or 177.4% more work to interpolate a new  
string.

Sure the numbers are small, but then how many times might a typical  
program do this kind of little extra effort?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
