[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: help learning arrays _Thanks

Peter Szinek

11/13/2006 7:22:00 AM

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.rubyra...