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

···

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

Hi --

···

On Tue, 21 Aug 2007, Marcin Tyman wrote:

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)

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

felix@felix-laptop:~$ cat demo.xml
<classes purpose="test">
  <class name="testclass">
    1
  </class>
</classes>
felix@felix-laptop:~$ irb
irb(main):001:0> require 'rexml/document'
=> true
irb(main):002:0> xml = REXML::Document.new(File.open('demo.xml'))
=> <UNDEFINED> ... </>
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>

···

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

Thanks for any help.
MT
--
Posted via http://www.ruby-forum.com/\.

David A. Black wrote:

Hi --

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 Tue, 21 Aug 2007, Marcin Tyman wrote:

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

Hi --

···

On Tue, 21 Aug 2007, Marcin Tyman wrote:

David A. Black wrote:

Hi --

On Tue, 21 Aug 2007, Marcin Tyman 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

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

--
* 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)

How about: arr << "#{el.elements["id"].get_text}".to_i ?

···

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

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 ?

If you ever are tempted to write:
"#{foo}"
you should realize that it's functionally equivalent to
foo.to_s

···

On Aug 21, 7:12 am, Kaldrenon <kaldre...@gmail.com> wrote:

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 ?

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

···

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

On Aug 21, 7:12 am, Kaldrenon <kaldre...@gmail.com> wrote:

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 ?

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, 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

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 8/21/07, Kaldrenon <kaldrenon@gmail.com> wrote:

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. :slight_smile: I just think
"#{foo}" looks busier than foo.to_s

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

···

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

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

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__

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
Rob@AgileConsultingLLC.com

···

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

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

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?