Instance variable/ I'm lost

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 :frowning:

Begin forwarded message:

multiplication problems to keep my mind fresh;
def multiply

a seperate def and create and instance variable like
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 :frowning:

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

On 2 Oct 2007, at 11:21, Erik B. wrote:

multiple2 = rand(11)
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.

Instance variables are for (as you note below) classes. You don’t
have a class so you don’t need to use instance variables.

The simplest solution here would be to use a global ($correct),
though globals are usually frowned upon in larger programs:

puts ‘How many problems do you want to solve?’
problems = gets.chomp

$correct = 0

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(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 += 1
else puts “You fail!”
end
end

problems.to_i.times do multiply
end

puts “You scored #{$correct}/#{problems}”

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 :frowning:

I don’t know what confusing book you are reading, but their are many
excellent tutorials for Ruby which will explain these concepts better
than I can. See any of the recent ‘I’m a beginner what book should I
read?’ threads.

Alex G.

Bioinformatics Center
Kyoto University

Instance variables start with @
so you want :
@correct or @number_correct

to increment it by one:
@correct += 1

ok… do you think you could put it in the code, because i dont now how
to do stuff like initialize the instance variable if you even have to,
i think you do w/ classes right? :(?

Hi,

Welcome!

On 1-Oct-07, at 10:21 PM, Erik B. wrote:

puts "The test will beging #{x} in seconds! "
if answer.to_s == answerp.to_s
correctly. I tried putting in the definition after the if statement

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!

As you say, an instance variable would work, this is written as:
@correct – I’ve inserted these below (unless I edited this
incorrectly, it should work).

puts ‘How many problems do you want to solve?’
problems = gets.chomp

@correct = 0

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(11)
answer = multiple1 * multiple2

puts ‘what is ’ + multiple1.to_s + ’ * ’ + multiple2.to_s + ’ ?’

answerp = gets.chomp
if answer.to_s == answerp.to_s then
puts “good job”
@correct = @correct + 1
else
puts “You fail!”
end
end

problems.to_i.times do
multiply
end

puts “correct: #{@correct}”

Now you’ll be asking yourself: so what is the instance that this is a
variable of? And: what’s running this program? Good questions. You’ll
need to learn about classes, objects, and scopes to answer them.

There is an alternative technique using parameters and returned values:

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(correct)
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 then
puts “good job”
correct = correct + 1
else
puts “You fail!”
end
return correct
end

correct = 0
problems.to_i.times do
correct = multiply(correct)
end

puts “correct: #{correct}”

Another thing I should mention, since you are just starting out… it
is best if you are very rigourous about your code from the very
beginning. This includes things like formatting your code. Some
habits are hard to un-learn, so don’t form them :slight_smile:

Have fun!

Cheers,
Bob

ps: sorry long post :frowning:

Posted via http://www.ruby-forum.com/.


Bob H. – tumblelog at http://
www.recursive.ca/so/
Recursive Design Inc. – weblog at http://www.recursive.ca/
hutch
http://www.recursive.ca/ – works on http://www.raconteur.info/
cms-for-static-content/home/

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
:frowning: what do i need to fix?

On 1-Oct-07, at 10:46 PM, Alex G. wrote:

Instance variables are for (as you note below) classes. You don’t
have a class so you don’t need to use instance variables.

Actually instance variables will work fine. See my other post.

The simplest solution here would be to use a global ($correct),
though globals are usually frowned upon in larger programs:

:frowning:

Even though frown inducing, you’re right, that’ll work too.

Cheers,
Bob


Bob H. – tumblelog at http://
www.recursive.ca/so/
Recursive Design Inc. – weblog at http://www.recursive.ca/
hutch
http://www.recursive.ca/ – works on http://www.raconteur.info/
cms-for-static-content/home/

. You can

just access the @correct variable directly as Bob wrote:

@correct = @correct + 1
Sorry i dont see where, bob wrote that =|.
ok i understand everyones ways of going about this except bobs ways of
using parameters and returned values is messing me up a bit. So heres my
finished product, if anyone sees anything usless or somthing im doing
wrong, please let me know :slight_smile:

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
@correct = 0
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 = @correct + 1
else puts “You fail!”
end
end

problems.to_i.times do multiply
end

puts correct.to_i

On 2 Oct 2007, at 13:14, Erik B. wrote:

. You can

just access the @correct variable directly as Bob wrote:

@correct = @correct + 1
Sorry i dont see where, bob wrote that =|.

It is in the first example he gave in his first reply.

puts "The test will beging #{x} in seconds! "
@correct = 0
end
end

problems.to_i.times do multiply
end

puts correct.to_i

You still have two methods defined which do (almost) nothing now. You
call one of them on the final line, but it isn’t necessary because it
just returns @correct. Apart from that it is OK, I would rearrange
the various to_i and to_s methods and a few other bits to make it
(IMO) a bit neater and Ruby-ish:

puts ‘How many problems do you want to solve?’
problems = gets.chomp.to_i

5.downto(1) do |x|
puts “The test will begin in #{x} seconds!”
sleep 1
end

puts ‘Start!’

@correct = 0

def multiply
multiple2 = rand(11)
multiple1 = rand(11)
answer = multiple1 * multiple2
puts “What is #{multiple1} * #{multiple2} ?”
answerp = gets.chomp.to_i
if answer == answerp
puts “Good job”
@correct = @correct + 1
else
puts “You fail!”
end
end

problems.times do
multiply
end

puts @correct

Again though, I wouldn’t use instance variables in this case, but
something like this which is slightly longer, but a bit clearer (for
me anyway):

puts ‘How many problems do you want to solve?’
problems = gets.chomp.to_i

5.downto(1) do |x|
puts “The test will begin in #{x} seconds!”
sleep 1
end

puts ‘Start!’

def problem_answered?
multiple2 = rand(11)
multiple1 = rand(11)
answer = multiple1 * multiple2
puts “What is #{multiple1} * #{multiple2} ?”
answerp = gets.chomp.to_i
if answer == answerp
puts “Good job”
return true
else
puts “You fail!”
return false
end
end

correct = 0

problems.times do
if problem_answered?
correct += 1
end
end

puts correct

Alex G.

Bioinformatics Center
Kyoto University

On 2 Oct 2007, at 12:10, Erik B. wrote:

puts ‘Start!’
multiple1 = rand(11)
problems.to_i.times do multiply
end

puts correct.to_i

for some reason the variable wont change it just returns nil each time
:frowning: 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 G.

Bioinformatics Center
Kyoto University

OK, final post. Here is a version using a class and instance variables:

class Test
def initialize
@correct = 0
@total = 0
end
def pose_multiplication_problem
@total += 1
multiple2 = rand(11)
multiple1 = rand(11)
answer = multiple1 * multiple2
puts “What is #{multiple1} * #{multiple2} ?”
answerp = gets.chomp.to_i
if answer == answerp
puts “Good job”
@correct += 1
else
puts “Sorry, you fail”
end
end
def score
“#{@correct}/#{@total}”
end
end

puts ‘How many problems do you want to solve?’
this_many = gets.chomp.to_i

test = Test.new
this_many.times{ test.pose_multiplication_problem }
puts “You scored #{test.score}”

Alex G.

Bioinformatics Center
Kyoto University

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs