I’m new to both Ruby and programming in general.
I’ve been trying to put together a little script that randomly creates math questions and works them out as well, however I’m having trouble generating a random math operator.
My script so far:
num = (1..10).to_a
num1 = num.shuffle[0]
num2 = num.shuffle[0]
op = %w{+ - = /}
op1 = op.sample
puts w = "#{num1} #{op1} #{num2}"
puts "Your answer is:"
answer = gets()
solution = num1 + "what do I put here??" + num2
if answer.to_i == solution
puts "Correct! The answer is #{solution}"
else
puts "Incorrect, the answer is #{solution}"
end
I’ve managed to pick out a random operator but can’t use it in the solution as it’s a string.
Why not use symbols, and send it to an operand with an argument?
For example:
Why’d you use
samplefor the ops, but not the numbers?