Seems so simple, but is this really the only way, or most directway?

J

John Pywtorak

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
 
P

Phrogz

John said:
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]
 
H

Harold Hausman

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
 
B

benjohn

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top