# How to do typecasting?

**URL:** <https://rubytalk.org/t/how-to-do-typecasting/925>\
**Category:** ruby-talk\
**Created:** [30 July 2002 21:13 UTC](https://rubytalk.org/t/how-to-do-typecasting/925 "2002-07-30T21:13:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel10](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Daniel10](https://rubytalk.org/u/Daniel10)\
**Post date:** [30 July 2002 21:13 UTC](https://rubytalk.org/t/how-to-do-typecasting/925/1 "2002-07-30T21:13:42Z")

</div>

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”…

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [30 July 2002 21:17 UTC](https://rubytalk.org/t/how-to-do-typecasting/925/2 "2002-07-30T21:17:07Z")

</div>

Try this fix:

a.each {|l| print l.to\_s.length }

Hal

> **···**
>
> ----- Original Message -----  
> From: “Daniel Bretoi” [lists@debonair.net](mailto:lists@debonair.net)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto: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?

---

<div class="post-metadata">

**Author:** ![Josh\_Huber1](https://avatars.discourse-cdn.com/v4/letter/j/3be4f8/32.png) [@Josh\_Huber1](https://rubytalk.org/u/Josh_Huber1)\
**Post date:** [30 July 2002 21:17 UTC](https://rubytalk.org/t/how-to-do-typecasting/925/3 "2002-07-30T21:17:41Z")

</div>

Daniel Bretoi [lists@debonair.net](mailto: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

---

<div class="post-metadata">

**Author:** ![Daniel10](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Daniel10](https://rubytalk.org/u/Daniel10)\
**Post date:** [30 July 2002 21:25 UTC](https://rubytalk.org/t/how-to-do-typecasting/925/4 "2002-07-30T21:25:04Z")

</div>

_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](mailto: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”…
