a=[1,2,3]
=> [1, 2, 3]
irb(main):008:0> b=[4,5,6]
=> [4, 5, 6]
irb(main):009:0> a[1]+b
TypeError: Array can’t be coerced into Fixnum
from (irb):9:in +' from (irb):9 from :0 irb(main):010:0> a[1].to_a+b (irb):10: warning: default
to_a’ will be obsolete
=> [2, 4, 5, 6]
is there better way to change string into array?
a[1].to_a will be obsolere
On Dec 31, 2010, at 00:09 , Pen T. wrote:
(irb):10: warning: default `to_a’ will be obsolete
=> [2, 4, 5, 6]
is there better way to change string into array?
a[1].to_a will be obsolere
What string?
Are you trying inserting an Integer at the begining of an Array?
On Fri, Dec 31, 2010 at 5:14 AM, Ryan D. [email protected]
wrote:
On Dec 31, 2010, at 00:09 , Pen T. wrote:
irb(main):009:0> a[1]+b
…
is there better way to change string into array?
a[1].to_a will be obsolere
Pen T. wrote in post #971595:
a=[1,2,3]
=> [1, 2, 3]
irb(main):008:0> b=[4,5,6]
=> [4, 5, 6]
irb(main):009:0> a[1]+b
TypeError: Array can’t be coerced into Fixnum
from (irb):9:in +' from (irb):9 from :0 irb(main):010:0> a[1].to_a+b (irb):10: warning: default
to_a’ will be obsolete
=> [2, 4, 5, 6]
is there better way to change string into array?
a[1].to_a will be obsolere
If you want to create the array [2,4,5,6] then
there are 2 proper ways:
b=[4,5,6]
=> [4, 5, 6]
b.unshift(a[1]) # insert this element at the front
=> [2, 4, 5, 6]
or
b=[4,5,6]
=> [4, 5, 6]
a[1…1]+b # a[1…1] creates the sub-array of from position 1 to 1
=> [2, 4, 5, 6]
And if you wanted the to_a functionality, then check Kernel.Array
(but that has unneeded conversions away from and back to an Array)
b=[4,5,6]
=> [4, 5, 6]
Array(a[1]) + b
=> [2, 4, 5, 6]
I assume you say “string” but really mean 2 (which is an object
of Fixnum class):
a[1].class
=> Fixnum
HTH,
Peter
Pen T. wrote in post #971595:
a=[1,2,3]
=> [1, 2, 3]
irb(main):008:0> b=[4,5,6]
=> [4, 5, 6]
irb(main):009:0> a[1]+b
TypeError: Array can’t be coerced into Fixnum
But you can concatenate two arrays, is this what you want?
[a[1]] + b