How to "cast" in Ruby

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

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

arr = Array.new()
Thanks for any help.
MT

Posted via http://www.ruby-forum.com/.

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

[email protected]:~$ cat demo.xml


1


[email protected]:~$ 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>

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 :slight_smile:

David

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!

On Aug 21, 8:13 am, Marcin T. [email protected] 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 ?

2007/8/21, Kaldrenon [email protected]:

On Aug 21, 8:13 am, Marcin T. [email protected] 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

On Aug 21, 7:12 am, Kaldrenon [email protected] wrote:

On Aug 21, 8:13 am, Marcin T. [email protected] 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

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.

On 8/21/07, Kaldrenon [email protected] wrote:

It’s also exactly the same number of characters.

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

On Aug 21, 11:15 am, Phrogz [email protected] 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.

On Aug 21, 10:11 pm, “Logan C.” [email protected] wrote:

On 8/21/07, Kaldrenon [email protected] wrote:

It’s also exactly the same number of characters.

I had to count to be sure, but you are indeed correct. :slight_smile: 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.

On Aug 22, 8:35 am, James Edward G. II [email protected]
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?

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

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
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs