# Help learning arrays \_Thanks

**URL:** <https://www.ruby-forum.com/t/help-learning-arrays-thanks/75752>\
**Category:** Ruby\
**Created:** [November 13, 2006, 5:25am UTC](https://www.ruby-forum.com/t/help-learning-arrays-thanks/75752 "2006-11-13T05:25:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 13, 2006, 5:25am UTC](https://www.ruby-forum.com/t/help-learning-arrays-thanks/75752/1 "2006-11-13T05:25:58Z")

</div>

Hi Iâ€™m learning arrays from a book. I have learned the  
basics of branching and, looping using a handful of objects and,  
methods. Now Iâ€™m  
suppose to use the skills I have acquired to gets a list of words and,  
when I  
hit the enter key on an empty line the list should be returned to me.

Hears what I have been trying recently.

list = []

while list != ‘’  
list = gets.chomp  
end  
puts list # any help will be appreciated.

Thanks

---

<div class="post-metadata">

**Author:** ![jamison\_edmonds](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/838e76/32.png) [@jamison\_edmonds](https://www.ruby-forum.com/u/jamison_edmonds)\
**Post date:** [November 13, 2006, 8:24am UTC](https://www.ruby-forum.com/t/help-learning-arrays-thanks/75752/2 "2006-11-13T08:24:12Z")

</div>

jamison edmonds wrote:

> list = []

OK. So the variable ‘list’ points to an empty array. So far so good.

> while list != ‘’  
> Now you are saying to do something until an array (‘list’) will be  
> unequal with an empty string. This will never happen IMHO - usually you  
> want to compare arrays to arrays and strings to strings (OK, this will  
> change later, but for now consider it to be so)

> ```
> list = gets.chomp
> 
> ```

This line changes the whole story. ‘list’ does not reference an array  
any more, but a string, whose value is - after this line is executed -  
the string the user types in.

If we now revisit the loop condition

while list != ‘’

it makes more sense now, since ‘list’ is a string already, and the loop  
will exit if it is empty.

However, there are two problems: the reference to your original empty  
array is lost, and anyway, you are not putting anything in there.

How about this:

====================================  
list = []

while (input = gets.chomp) != ‘’ do  
list \<\< input  
end

# puts list

HTH,  
Peter

\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com)
