On 2 Oct 2007, at 12:10, Erik Boling wrote:

> alright wow thanks for all the posts, ok i understand ur guys's ways,
> but look at this
>
> 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 correct=(correct)
>     @correct = correct
>     end
>
>   def correct
>    @correct
>    end
>
>  def multiply
> multiple2 = rand(11)
> multiple1 = rand(11)
>   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"
>     correct.to_i + 1
>   else puts "You fail!"
>   end
> end
>
> problems.to_i.times do multiply
> end
>
> puts correct.to_i
>
> for some reason the variable wont change it just returns nil each time
> :( what do i need to fix?

First, you never set @correct. You should initialise it to 0 before  
you use it. In fact this doesn't matter in your program, but it's  
good practice anyway. Bob showed you how in his program.

Your main problem is you never call your 'correct=' method so  
@correct never gets updated. To fix it as you have written it you can  
replace

correct.to_i + 1

with

send(:correct=,correct.to_i + 1)

And it will work, but it is horrible style! Alternatively if you  
rename your method to something better (like 'set_correct') you can  
write:

set_correct(correct.to_i+1)

But all these methods are a bit unnecessary (perhaps you copied them  
from a book without really understanding why they are used?). You can  
just access the @correct variable directly as Bob wrote:

@correct = @correct + 1

Alex Gutteridge

Bioinformatics Center
Kyoto University