# Noob Questions

**URL:** <https://www.ruby-forum.com/t/noob-questions/134925>\
**Category:** Ruby\
**Created:** [April 16, 2008, 7:31am UTC](https://www.ruby-forum.com/t/noob-questions/134925 "2008-04-16T07:31:00Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 16, 2008, 7:31am UTC](https://www.ruby-forum.com/t/noob-questions/134925/1 "2008-04-16T07:31:00Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 16, 2008, 7:44am UTC](https://www.ruby-forum.com/t/noob-questions/134925/2 "2008-04-16T07:44:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 16, 2008, 8:25am UTC](https://www.ruby-forum.com/t/noob-questions/134925/3 "2008-04-16T08:25:30Z")

</div>

On Apr 15, 10:43 pm, Justin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 17, 2008, 5:34am UTC](https://www.ruby-forum.com/t/noob-questions/134925/4 "2008-04-17T05:34:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 16, 2008, 8:51am UTC](https://www.ruby-forum.com/t/noob-questions/134925/5 "2008-04-16T08:51:58Z")

</div>

From: calderson [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 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

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 17, 2008, 12:33pm UTC](https://www.ruby-forum.com/t/noob-questions/134925/6 "2008-04-17T12:33:11Z")

</div>

On Apr 16, 9:55 am, RoRNoob [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 17, 2008, 12:03pm UTC](https://www.ruby-forum.com/t/noob-questions/134925/7 "2008-04-17T12:03:45Z")

</div>

On Apr 16, 3:25 am, Ron F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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!

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 17, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/noob-questions/134925/8 "2008-04-17T16:52:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rornoob](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@rornoob](https://www.ruby-forum.com/u/rornoob)\
**Post date:** [April 17, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/noob-questions/134925/9 "2008-04-17T17:03:53Z")

</div>

On Apr 16, 2:49 pm, Justin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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!
