[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to "cast" in Ruby

Marcin Tyman

8/21/2007 12:00:00 PM

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

13 Answers

David A. Black

8/21/2007 12:05:00 PM

0

Marcin Tyman

8/21/2007 12:13:00 PM

0

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

Thanks for directing!
--
Posted via http://www.ruby-....

David A. Black

8/21/2007 12:25:00 PM

0

Kaldrenon

8/21/2007 1:12:00 PM

0

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 ?

Robert Klemme

8/21/2007 2:19:00 PM

0

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 ?

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

Phrogz

8/21/2007 3:16:00 PM

0

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

John Joyce

8/21/2007 5:38:00 PM

0


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

Kaldrenon

8/21/2007 5:55:00 PM

0

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

Logan Capaldo

8/22/2007 2:12:00 AM

0

On 8/21/07, Kaldrenon <kaldrenon@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
>
>

Kaldrenon

8/22/2007 12:29:00 PM

0

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.