Noob Questions

Be warned - I’m new to Ruby and programming in general, so I’m sure
this will be a trivial question for most of you.

I’m currently working through the exercises in the book Learn to
Program by Chris P… At the end of each chapter Chris has a little
programming problem that the reader is asked to solve. I’ve been able
to follow along just fine up until tonight.

The problem in question is at the end of Chapter 8 (Arrays and
Iterators): I have to write a program that asks the user to type in as
many words as they want (one word per line, continuing until they just
press Enter on an empty line) and then repeat the words back to them
in alphabetical order.

The program lives in a .rb file and is executed and ran from the
terminal.

Here is my code:

alphalist = []
alphalist.each do |item|
puts ‘type words one at a time and I will alphabetize them for you.’
alphalist.push gets.chomp
end
puts alphalist.sort

When I run the .rb file, nothing happens. Any help is appreciated!

RoRNoob wrote:

many words as they want (one word per line, continuing until they just
puts ‘type words one at a time and I will alphabetize them for you.’
alphalist.push gets.chomp
end
puts alphalist.sort

When I run the .rb file, nothing happens. Any help is appreciated!

Take a look at your alphalist: it’s just an empty array. When you do
alphalist.each, you are ‘visiting’ each item in the array, but there are
none. So it just skips down to ‘puts alphalist.sort’ and then exits.

-Justin

On Apr 15, 10:43 pm, Justin C. [email protected] wrote:

Iterators): I have to write a program that asks the user to type in as
alphalist.each do |item|

-Justin

OK that helped - I’m able to get the array to load up and print out
with this:

alphalist = [‘’]
alphalist.each do |item|
puts ‘type words one at a time and I will alphabetize them for you.’
alphalist.push gets.chomp
puts alphalist.sort
end

But now I can’t figure out how to keep it from printing until the user
enters an empty value. I’ve tried:

alphalist = [‘’]
alphalist.each do |item|
puts ‘type words one at a time and I will alphabetize them for you.’
alphalist.push gets.chomp
if gets == ‘’
puts alphalist.sort
end
end

… it prints a gets value twice, then starts again with “type words
one at a time and I will alphabetize them for you.”

If i hit enter it just puts a blank line.

Approach it as follows;

  1. A loop that is not a .each loop that reads in user input
    until the input is an empty line (that is size 0 after being
    chomped)
  2. Then sort the array.
  3. Then a .each iteration through the array to output the contents.

RF

From: calderson [mailto:[email protected]]

alphalist = [‘’]

alphalist.each do |item|

puts ‘type words one at a time and I will alphabetize them for you.’

alphalist.push gets.chomp

you run gets here ^^^^

if gets == ‘’

and here ^^^

that will execute gets twice
the gets == ‘’ will never be true so the ff line is never run

puts alphalist.sort

end

end

… it prints a gets value twice, then starts again with "type words

one at a time and I will alphabetize them for you."

If i hit enter it just puts a blank line.

how about trying something simple

alphalist = []
loop do
puts ‘type words one at a time and I will sort them for you.’
input = gets.strip
exit if input.empty?
puts “-----o-----”
puts alphalist.push(input).sort
end

kind regards -botp

On Apr 16, 9:55 am, RoRNoob [email protected] wrote:

RF

The problem in question is at the end of Chapter 8 (Arrays and
alphalist = []
alphalist.each do |item|
puts ‘type words one at a time and I will alphabetize them for you.’
alphalist.pushgets.chomp
end
puts alphalist.sort

When I run the .rb file, nothing happens. Any help is appreciated!

Cool - I will give that a try - thanks for your time everyone!

OK progress:

alphalist = [‘’]
input = ‘’

while input != ‘bye’
puts ‘type words one at a time and I will alphabetize them for you.’
input = gets.chomp
if input != ‘bye’
alphalist.push input
end
end

puts alphalist.sort


My problem is that I can’t figure out how to make it exit the loop if
the user submits a 0 length string. The only way I’ve been able to get
it to work is to watch for ‘bye’ - which I like better than the empty
string, but the problem from the book calls for an empty string loop
exit

On Apr 16, 3:25 am, Ron F. [email protected] wrote:

this will be a trivial question for most of you.
in alphabetical order.
end
puts alphalist.sort

When I run the .rb file, nothing happens. Any help is appreciated!

Cool - I will give that a try - thanks for your time everyone!

RoRNoob wrote:

  1. Then sort thearray.

Program by Chris P… At the end of each chapter Chris has a little
terminal.
When I run the .rb file, nothing happens. Any help is appreciated!
puts ‘type words one at a time and I will alphabetize them for you.’

My problem is that I can’t figure out how to make it exit the loop if
the user submits a 0 length string. The only way I’ve been able to get
it to work is to watch for ‘bye’ - which I like better than the empty
string, but the problem from the book calls for an empty string loop
exit

I suspect your difficulty comes from the fact that you are setting input
to the empty string before entering the while loop. One approach would
be to start off with input as something else, like nil, then doing while
input != “”

-Justin

On Apr 16, 2:49 pm, Justin C. [email protected] wrote:

to follow along just fine up until tonight.
Here is my code:
Cool - I will give that a try - thanks for your time everyone!
alphalist.push input
string, but the problem from the book calls for an empty string loop
exit

I suspect your difficulty comes from the fact that you are setting input
to the empty string before entering the while loop. One approach would
be to start off with input as something else, like nil, then doing while
input != “”

-Justin

Solved - Thanks for the suggestion Justin. For any other noobs - here
is the solution:

alphalist = [‘’]
input = nil

while input != “”
puts ‘type words one at a time and I will alphabetize them for you.’
input = gets.chomp
alphalist.push input
end

puts alphalist.sort

Cheers!