# Newbie question

**URL:** <https://www.ruby-forum.com/t/newbie-question/84710>\
**Category:** Ruby\
**Created:** [February 7, 2007, 4:50am UTC](https://www.ruby-forum.com/t/newbie-question/84710 "2007-02-07T04:50:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [February 7, 2007, 4:50am UTC](https://www.ruby-forum.com/t/newbie-question/84710/1 "2007-02-07T04:50:45Z")

</div>

I am very new to programming “infant new”  
and I have a question about a return I am scratching my head over in a  
simple program I am writing

here is my code… shouldn’t the return of my method be “true” after  
pressing enter and thus end the loop?

while (quit == false)  
def check\_if\_blank input  
if(input == ‘’)  
quit = true  
else  
quit = false  
end  
quit  
end  
puts ‘Enter a number’  
first = gets.chomp  
quit = check\_if\_blank first  
first\_number.push first.to\_i

```
puts 'good..'
puts 'now enter a number to multiply the first number by'

second = gets.chomp
quit = check_if_blank second
second_number.push second.to_i
answer.push first_number[(first_number.length) -1] *

```

second\_number[(second\_number.length) -1]

```
puts first_number[(first_number.length) -1].to_s + ' * ' +

```

second\_number[(second\_number.length) -1].to\_s + ’ = ’ +  
answer[(answer.length) -1].to\_s  
if(first\_number.length == 5)  
quit = true  
end  
end

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [February 7, 2007, 5:27am UTC](https://www.ruby-forum.com/t/newbie-question/84710/2 "2007-02-07T05:27:47Z")

</div>

I had to work to make your code run. Here’s the deal. The while loop  
is  
initialized and then it asks for their input. If their input is  
nothing,  
then it goes through the rest of the code in the while loop, finishes  
the  
while loop and then the value quit is checked again. at that point the  
value quit == true, and so the loop ends. Does that make sense?

Also, if someone puts in a blank carriage return on the first one and  
then a  
number on the second, the value continues to be false.

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [February 7, 2007, 6:08am UTC](https://www.ruby-forum.com/t/newbie-question/84710/3 "2007-02-07T06:08:49Z")

</div>

Thank you Jason,  
That process had skipped my mind for some reason I thought just  
setting the variable to true would kill the loop.  
this is my revised code.

My first program without just typing out an example from a book

here we go.

any suggestions on how to clean it up would be great.

* * *

first\_number = []  
second\_number = []  
answer = []  
quit = false

def check\_if\_blank input  
if(input == ‘’)  
return true  
else  
return false  
end  
end

while (quit == false)  
if(quit == false)  
puts ‘Enter a number’  
first = gets.chomp  
first\_test = check\_if\_blank first  
if(first\_test == false)  
first\_number.push first.to\_i  
else  
quit = true  
end  
end  
if(first\_test == false)  
puts ‘…good.’  
puts ‘Now enter a number to multiply to the first number’  
second = gets.chomp  
second\_test = check\_if\_blank second  
if(second\_test == false)  
second\_number.push second.to\_i  
answer.push first\_number[(first\_number.length) -1] \*  
second\_number[(second\_number.length) -1]  
puts first\_number[(first\_number.length) -1].to\_s + ’ \* ’

- second\_number[(second\_number.length) -1].to\_s + ’ = ’ +  
answer[(answer.length) -1].to\_s  
else  
quit = true

if(second\_test == false)  
finished = false  
amount\_of\_answers = 0  
puts  
puts ‘okay now we are going to look at all the questions your asked  
and their respective answers’  
while(finished != true)  
if(amount\_of\_answers == first\_number.length)  
finished = true  
else  
puts first\_number[amount\_of\_answers].to\_s + ’ \* ’ +  
second\_number[amount\_of\_answers].to\_s + ’ = ’ +  
answer[amount\_of\_answers].to\_s  
amount\_of\_answers += 1  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [February 7, 2007, 6:36am UTC](https://www.ruby-forum.com/t/newbie-question/84710/4 "2007-02-07T06:36:52Z")

</div>

On 2/7/07, Vincent Gabriel F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you Jason,  
> That process had skipped my mind for some reason I thought just  
> setting the variable to true would kill the loop.  
> this is my revised code.
> 
> My first program without just typing out an example from a book
> 
> here we go.
> 
> any suggestions on how to clean it up would be great.

I had a little trouble understanding what your code is supposed to do,  
but this code will keep asking you for numbers until you answer with  
an empty response (just hit enter)

It shows the equations as you enter them, and then in the end gives  
you a summary of the one’s you’ve entered. It does not account for  
all possible errors.

Look up String#empty, Array#\<\<, break, loop, and  
Array#each\_with\_index to understand a bit better what’s going on.

Welcome to Ruby. You might try out [http://tryruby.hobix.com](http://tryruby.hobix.com) as well  
as get acquainted with the documentation at [http://ruby-doc.org](http://ruby-doc.org)

Glad to see you hacking, newbie or not 🙂

firsts = []  
seconds = []  
answers = []  
loop do  
puts ‘Enter a number’  
first = gets.chomp  
break if first.empty?  
firsts \<\< first\_number = first.to\_i  
puts ‘…good.’  
puts ‘Now enter a number to multiply to the first number’  
second = gets.chomp  
break if second.empty?  
seconds \<\< second\_number = second.to\_i  
answers \<\< answer = (first\_number \* second\_number)  
puts “#{first\_number} \* #{second\_number} = #{answer}”  
end

puts ‘okay now we are going to look at all the questions your asked’  
answers.each\_with\_index do |c,i|  
puts “#{firsts[i]} \* #{seconds[i]} = #{c}”  
end

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [February 8, 2007, 3:46pm UTC](https://www.ruby-forum.com/t/newbie-question/84710/5 "2007-02-08T15:46:25Z")

</div>

Thanks for the links Gregory!  
nice to have a warm welcome instead of a flame  
hello world of ruby
