From: John Joyce Date: 2007-10-02T11:32:50+09:00 Subject: Fwd: instance variable/ I'm lost --Apple-Mail-1-384294654 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Begin forwarded message: > From: Erik Boling > Date: October 1, 2007 9:21:08 PM CDT > To: ruby-talk@ruby-lang.org (ruby-talk ML) > Subject: instance variable/ I'm lost > Reply-To: ruby-talk@ruby-lang.org > > Hello i am new to programming/ ruby but i have had some help from a > cousin and read a book so i have some knowlage of what I'm doing. > Anyways in my free time i was creating a program that tested me on > easy > multiplication problems to keep my mind fresh; > > puts 'How many problems do you want to solve?' > problems = gets.chomp > > 5.downto(1) do |x| > puts "The test will beging #{x} in seconds! " > sleep 1 > end > puts 'Start!' > > def multiply > multiple2 = rand(11) > multiple1 = rand(11S) > answer = multiple1 * multiple2 > puts 'what is ' + multiple1.to_s + ' * ' + multiple2.to_s + ' ?' > answerp = gets.chomp > if answer.to_s == answerp.to_s > puts "good job" > else puts "You fail!" > end > end > > problems.to_i.times do multiply > end > > : > So i was wondering, how can i tally up how many times they answer > correctly. I tried putting in the definition after the if statement > correct + 1, * obviously i made the variable "correct" above* but the > problem is, i cant retrive the variable out of the definition i had > made. which brings me to my question of instance variables. Can't i > make > a seperate def and create and instance variable like > def correct > @correct = correct > > then in my multiplication def. under the if statement do > correct + 1 > > then finally at the end do > puts correct > and that would give me the total correct, but thats not working > for me? > i dont know what im doing wrong? I see these instance variable in this > confusing book i just started to read, and it looks like maybe there > needs to be a class?!?!? idk but if someone could help me get this > stuff > figured out that would be awesome! > ps: sorry long post :( > -- > Posted via http://www.ruby-forum.com/. > Instance variables start with @ so you want : @correct or @number_correct to increment it by one: @correct += 1 --Apple-Mail-1-384294654--