[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

newbie question

Vincent Gabriel Franco

2/7/2007 3:50:00 AM

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


3 Answers

Vincent Gabriel Franco

2/7/2007 5:07:00 AM

0

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

end
end

if(second_number.length == 5)
quit = true
end
end

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


-------------------------------------------------------------------

Gregory Brown

2/7/2007 5:37:00 AM

0

On 2/7/07, Vincent Gabriel Franco <vincent-franco@comcast.net> 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... as well
as get acquainted with the documentation at http://ru...

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

Vincent Gabriel Franco

2/8/2007 2:45:00 PM

0

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


Gregory Brown wrote:
>
> 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... as well
> as get acquainted with the documentation at http://ru...
>
> 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
>
>