trans string into array

Discussion in 'Ruby' started by Pen Ttt, Dec 31, 2010.

  1. Pen Ttt

    Pen Ttt Guest

    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/.
     
    Pen Ttt, Dec 31, 2010
    #1
    1. Advertising

  2. Pen Ttt

    Ryan Davis Guest

    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?
     
    Ryan Davis, Dec 31, 2010
    #2
    1. Advertising

  3. Pen Ttt

    Abinoam Jr. Guest

    Are you trying inserting an Integer at the begining of an Array?

    On Fri, Dec 31, 2010 at 5:14 AM, Ryan Davis <> 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?
     
    Abinoam Jr., Dec 31, 2010
    #3
  4. 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/.
     
    Peter Vandenabeele, Dec 31, 2010
    #4
  5. 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/.
     
    Brian Candler, Jan 1, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jonathan N. Little
    Replies:
    13
    Views:
    5,706
    Jonathan N. Little
    Feb 6, 2005
  2. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,812
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  3. rob
    Replies:
    4
    Views:
    3,067
  4. Anthony Bollinger

    Trans from ASP to ASP.NET 2.0 via 1.0

    Anthony Bollinger, May 12, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    399
    Anthony Bollinger
    May 14, 2006
  5. clyyy2002
    Replies:
    1
    Views:
    114
    Chad Z. Hower aka Kudzu
    May 14, 2005
Loading...

Share This Page