How to do typecasting?

hi,

is there a way to typecast values? 
if I have an array with numbers, and I want to perform sting type
methods on it.

a = [1,2,4345,3]
a.each { |l| print l.length }
to produce
>>  1141

besides defining a as ["1","2",etc], what options are there?
···


“Then we typed the G, and the system crashed”…

Try this fix:

a.each {|l| print l.to_s.length }

Hal

···

----- Original Message -----
From: “Daniel Bretoi” lists@debonair.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, July 30, 2002 4:13 PM
Subject: how to do typecasting?

if I have an array with numbers, and I want to perform sting type
methods on it.

a = [1,2,4345,3]
a.each { |l| print l.length }
to produce

1141

besides defining a as [“1”,“2”,etc], what options are there?

Daniel Bretoi lists@debonair.net writes:

is there a way to typecast values?
if I have an array with numbers, and I want to perform sting type
methods on it.

a = [1,2,4345,3]
a.each { |l| print l.length }
to produce

1141

besides defining a as [“1”,“2”,etc], what options are there?

a.each { |v|
print v.to_s.length
}

a = %w[1 2 4345 3]
=> [“1”, “2”, “4345”, “3”]

hth,

···


Josh Huber

smackself
I must be dizze since my last selfsmack…
thanks guys

db

···

On Wed, Jul 31, 2002 at 06:17:41AM +0900, Josh Huber wrote:

Daniel Bretoi lists@debonair.net writes:

is there a way to typecast values?
if I have an array with numbers, and I want to perform sting type
methods on it.

a = [1,2,4345,3]
a.each { |l| print l.length }
to produce

1141

besides defining a as [“1”,“2”,etc], what options are there?

a.each { |v|
print v.to_s.length
}

a = %w[1 2 4345 3]
=> [“1”, “2”, “4345”, “3”]

hth,


Josh Huber


“Then we typed the G, and the system crashed”…