Help learning arrays _Thanks

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

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