Chris Pine - Section 8.3

Hello - I’m super new to Ruby and coding in general and perhaps a stupid
question, but I wanted to see if somebody can give me the explanation
for this:

Building and sorting an array. Write the program we talked about
at the beginning of this chapter, one that asks us to type as many
words as we want (one word per line, continuing until we just
press Enter on an empty line) and then repeats the words back
to us in alphabetical order. Make sure to test your program thoroughly;
for example, does hitting Enter on an empty line always
exit your program? Even on the first line? And the second? Hint:
There’s a lovely array method that will give you a sorted version of
an array: sort. Use it!

puts “Type in as many words as you want! When finished, press “Enter”
on an empty line!”
x = 0
word = “word1”
array = []

while word != “”
word = gets.chomp
array[x] = word
x = x + 1

puts “’”
puts array.sort

I’m understanding this for the most part, but I’m not sure how (word =
“word1”) fits in here… why do you have to assign word as such?

Thank you!


According to your condition it can’t be empty character,

word = “word1”
array = []

#####while word != “”#####

Look at here you are checking word !="" so If it’s empty, loop would be
termination at first iteration.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs