-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Marcin Tyman
Sent: Tuesday, August 21, 2007 5:00 AM
To: ruby-talk ML
Subject: 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?
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
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
···
2007/8/21, Kaldrenon <kaldrenon@gmail.com>:
On Aug 21, 8:13 am, Marcin Tyman <m.ty...@interia.pl> 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 ?
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, 11:15 am, Phrogz <phr...@mac.com> 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
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 21, 10:11 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:
On 8/21/07, Kaldrenon <kaldre...@gmail.com> 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
On Aug 21, 10:11 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:
On 8/21/07, Kaldrenon <kaldre...@gmail.com> 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.
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, 8:35 am, James Edward Gray II <ja...@grayproductions.net> wrote:
On Aug 22, 2007, at 7:29 AM, Kaldrenon wrote:
> On Aug 21, 10:11 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:
>> On 8/21/07, Kaldrenon <kaldre...@gmail.com> 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.
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
On Aug 22, 8:35 am, James Edward Gray II <ja...@grayproductions.net> > wrote:
On Aug 22, 2007, at 7:29 AM, Kaldrenon wrote:
On Aug 21, 10:11 pm, "Logan Capaldo" <logancapa...@gmail.com> wrote:
On 8/21/07, Kaldrenon <kaldre...@gmail.com> 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.
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?