# Assign the array returned by String.split to a variable

**URL:** <https://www.ruby-forum.com/t/assign-the-array-returned-by-string-split-to-a-variable/169434>\
**Category:** Ruby\
**Created:** [June 3, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/assign-the-array-returned-by-string-split-to-a-variable/169434 "2009-06-03T18:35:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![catsquotl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@catsquotl](https://www.ruby-forum.com/u/catsquotl)\
**Post date:** [June 3, 2009, 6:35pm UTC](https://www.ruby-forum.com/t/assign-the-array-returned-by-string-split-to-a-variable/169434/1 "2009-06-03T18:35:47Z")

</div>

Hi

## i have the following method

## def create length = @txt.length i = 0 while i \< (length + 1) line = @txt[i] 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

---

<div class="post-metadata">

**Author:** ![catsquotl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@catsquotl](https://www.ruby-forum.com/u/catsquotl)\
**Post date:** [June 3, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/assign-the-array-returned-by-string-split-to-a-variable/169434/2 "2009-06-03T18:45:32Z")

</div>

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

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

–output:–  
[“hello”, “world”]

---

<div class="post-metadata">

**Author:** ![catsquotl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@catsquotl](https://www.ruby-forum.com/u/catsquotl)\
**Post date:** [June 3, 2009, 6:42pm UTC](https://www.ruby-forum.com/t/assign-the-array-returned-by-string-split-to-a-variable/169434/3 "2009-06-03T18:42:23Z")

</div>

Catsquotl wrote:

> this works

Prove it.

---

<div class="post-metadata">

**Author:** ![catsquotl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@catsquotl](https://www.ruby-forum.com/u/catsquotl)\
**Post date:** [June 3, 2009, 6:49pm UTC](https://www.ruby-forum.com/t/assign-the-array-returned-by-string-split-to-a-variable/169434/4 "2009-06-03T18:49:58Z")

</div>

On Wed, Jun 3, 2009 at 9:35 AM, Catsquotl [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![catsquotl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f9ae1b/32.png) [@catsquotl](https://www.ruby-forum.com/u/catsquotl)\
**Post date:** [June 3, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/assign-the-array-returned-by-string-split-to-a-variable/169434/5 "2009-06-03T21:28:42Z")

</div>

Catsquotl wrote:

> Hi
> 
> ## i have the following method
> 
> ## def create length = @txt.length i = 0 while i \< (length + 1) line = @txt[i] 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.
