assign the array returned by String.split to a variable

Discussion in 'Ruby' started by Catsquotl, Jun 3, 2009.

  1. Catsquotl

    Catsquotl Guest

    Hi

    i have the following method
    -----
    def create
    length = @txt.length
    i = 0
    while i < (length + 1)
    line = @txt
    line.split(',')
    #@arr.push(a)
    i += 1
    end
    end
    -------
    this works but as soon as i take out the comment.
    i get an error something about private method split beeing called on a
    nil object.
    I found out that this has to do with split that has to be called by self
    or something.

    what i like to do is have an array of the split arrays.
    any ideas?

    greet Eelco
     
    Catsquotl, Jun 3, 2009
    #1
    1. Advertising

  2. Catsquotl

    7stud -- Guest

    7stud --, Jun 3, 2009
    #2
    1. Advertising

  3. Catsquotl

    7stud -- Guest

    > assign the array returned by String.split to a variable

    str = "hello,world"
    arr = str.split(",")
    p arr

    --output:--
    ["hello", "world"]
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Jun 3, 2009
    #3
  4. [Note: parts of this message were removed to make it a legal post.]

    On Wed, Jun 3, 2009 at 9:35 AM, Catsquotl <> wrote:

    > Hi
    >
    >
    >
    > what i like to do is have an array of the split arrays.
    > any ideas?
    >



    You would want something like this

    def create
    ary = @txt.collect{ |t| t.split(',') }
    end

    Basically collect takes your initial array and runs through each element and
    creates a new array based on the results of the block.

    John
     
    John W Higgins, Jun 3, 2009
    #4
  5. Catsquotl wrote:
    > Hi
    >
    > i have the following method
    > -----
    > def create
    > length = @txt.length
    > i = 0
    > while i < (length + 1)
    > line = @txt
    > line.split(',')
    > #@arr.push(a)
    > i += 1
    > end
    > end
    > -------
    > this works but as soon as i take out the comment.
    > i get an error something about private method split beeing called on a
    > nil object.


    This means that @arr is nil, that is, you are doing

    nil.push(a)

    So you need to initialize it first:

    @arr = []

    There are a few other errors in your code, for example you didn't assign
    to a:

    a = line.split(',')
    @arr.push(a)

    and your loop should be while i < length, not while i < length+1. As has
    been pointed out, there are more ruby-like ways to do this loop.
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jun 3, 2009
    #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. Matt
    Replies:
    8
    Views:
    53,997
    saravanavsk
    Jan 25, 2010
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    737
    Alex Martelli
    Sep 17, 2004
  3. Sam Kong
    Replies:
    5
    Views:
    276
    Rick DeNatale
    Aug 12, 2006
  4. Stanley Xu
    Replies:
    2
    Views:
    707
    Stanley Xu
    Mar 23, 2011
  5. Hobo Salesman
    Replies:
    7
    Views:
    138
    Hobo Salesman
    Jun 17, 2006
Loading...

Share This Page