# Seems so simple, but is this really the only way, or most di

**URL:** <https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561>\
**Category:** Ruby\
**Created:** [October 18, 2006, 2:37am UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561 "2006-10-18T02:37:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 18, 2006, 2:37am UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561/1 "2006-10-18T02:37:08Z")

</div>

9 digits in a string “123456789”, convert to individual Fixnums  
[1,2,3,4,5,6,7,8,9], or as below

a, b, c, d, e, f, g, h, i = id.scan(/\d/).map(&:to\_i)

Simpler yet how about [“1”, “2”, …]. Is scan the only direct way?

I tried the “\*” mechanism, no such luck, and tried array slicing again  
no luck. TIA

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 18, 2006, 3:01am UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561/2 "2006-10-18T03:01:09Z")

</div>

John P. wrote:

> 9 digits in a string “123456789”, convert to individual Fixnums

irb(main):001:0\> s = “123456789”  
=\> “123456789”  
irb(main):002:0\> s.split(’’).map{ |n| n.to\_i }  
=\> [1, 2, 3, 4, 5, 6, 7, 8, 9]

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 18, 2006, 3:13am UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561/3 "2006-10-18T03:13:11Z")

</div>

id.unpack(‘c’ \* id.size).map { |x| x-48 } # without regex and to\_i  
conversions

Mike D.  
[http://www.rubywizards.com](http://www.rubywizards.com)

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 18, 2006, 3:15am UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561/4 "2006-10-18T03:15:11Z")

</div>

On 10/18/06, John P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 9 digits in a string “123456789”, convert to individual Fixnums  
> [1,2,3,4,5,6,7,8,9], or as below
> 
> a, b, c, d, e, f, g, h, i = id.scan(/\d/).map(&:to\_i)
> 
> Simpler yet how about [“1”, “2”, …]. Is scan the only direct way?

“123456789”.split( ‘’ ) #thats two single quotes. (:

hth,  
-Harold

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 18, 2006, 1:15pm UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561/5 "2006-10-18T13:15:09Z")

</div>

On 10/18/06, Mike D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> id.unpack(‘c’ \* id.size).map { |x| x-48 } # without regex and to\_i  
> conversions

which is great, but if we are golfing let us improve our handicap 😉  
id.unpack(“c\*”)…

Robert

–  
The reasonable man adapts himself to the world; the unreasonable one  
persists in trying to adapt the world to himself. Therefore all progress  
depends on the unreasonable man.

- George Bernhard Shaw

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 18, 2006, 2:47pm UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561/6 "2006-10-18T14:47:53Z")

</div>

> 9 digits in a string “123456789”, convert to individual Fixnums  
> [1,2,3,4,5,6,7,8,9], or as below
> 
> a, b, c, d, e, f, g, h, i = id.scan(/\d/).map(&:to\_i)
> 
> Simpler yet how about [“1”, “2”, …]. Is scan the only direct way?
> 
> I tried the “\*” mechanism, no such luck, and tried array slicing again  
> no luck. TIA

(1…9).to\_a

?

I’m not quite sure what you are trying to do, you see 🙂 Are you trying  
to actaully set variables? If you are, first I’d ask “why are you trying  
to set a range of instance variables”. After that, if you’re sure you do  
(and hell, “for fun” is an answer), then I’m sure that there is a method  
like instance\_variable\_set, only for manipulating local variables, but  
oddly, I don’t seem able to find it at the moment…

---

<div class="post-metadata">

**Author:** ![johnpetterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@johnpetterson](https://www.ruby-forum.com/u/johnpetterson)\
**Post date:** [October 18, 2006, 1:51pm UTC](https://www.ruby-forum.com/t/seems-so-simple-but-is-this-really-the-only-way-or-most-di/72561/7 "2006-10-18T13:51:43Z")

</div>

> “123456789”.split( ‘’ ) #thats two single quotes. (:

But that only gives you nine strings. The question demanded nine  
Fixnums, which is the less elegant part.
