How to "cast" in Ruby

M

Marcin Tyman

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
 
D

David A. Black

Hi --

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?

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

arr << temp.to_i


David

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
M

Marcin Tyman

David said:
Hi --



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!
 
D

David A. Black

Hi --

David said:
Hi --



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

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

--
* Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)
 
K

Kaldrenon

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 ?
 
R

Robert Klemme

2007/8/21 said:
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
 
P

Phrogz

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
 
J

John Joyce

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
True, but for some (myself included) it definitely stands out more,
being more readable. But usage depends on context.
 
K

Kaldrenon

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.
 
L

Logan Capaldo

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
 
K

Kaldrenon

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.
 
J

James Edward Gray II

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.

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 Gray II
 
K

Kaldrenon

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__


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?
 
R

Rob Biedenharn

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,315
Members
48,001
Latest member
Wesley9486

Latest Threads

Top