Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > trans string into array

Reply
Thread Tools

trans string into array

 
 
Pen Ttt
Guest
Posts: n/a
 
      12-31-2010
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Ryan Davis
Guest
Posts: n/a
 
      12-31-2010

On Dec 31, 2010, at 00:09 , Pen Ttt wrote:

> 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


What string?



 
Reply With Quote
 
 
 
 
Abinoam Jr.
Guest
Posts: n/a
 
      12-31-2010
Are you trying inserting an Integer at the begining of an Array?

On Fri, Dec 31, 2010 at 5:14 AM, Ryan Davis <(E-Mail Removed)> wrot=
e:

> On Dec 31, 2010, at 00:09 , Pen Ttt wrote:
>> irb(main):009:0> a[1]+b

...
>> is there better way to =A0change string into array?
>> a[1].to_a will be obsolere


> What string?


 
Reply With Quote
 
Peter Vandenabeele
Guest
Posts: n/a
 
      12-31-2010
Pen Ttt 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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Brian Candler
Guest
Posts: n/a
 
      01-01-2011
Pen Ttt 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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trans Con Mobile announces mobile, Wi-Fi based VoIP system KnowingAbout.com Cisco 0 07-29-2006 06:51 PM
Trans from ASP to ASP.NET 2.0 via 1.0 Anthony Bollinger ASP .Net 3 05-13-2006 11:14 PM
trans.transform(xmlSource, htmlResult) fails rob XML 4 05-05-2006 02:14 PM
noscript trap redirection thats 4.01 trans compliant Jonathan N. Little HTML 13 02-06-2005 05:38 PM
Nikon CP5700 photos - Russia and the Trans-Siberian Railway Mads Pedersen Digital Photography 2 01-11-2004 09:23 AM



Advertisments