Additionally you could do something like
questions = [ “a slide is placed #{a=rand(3…15), a} cm to the left of a
lens.”
a could then be passed to some function.
questions =
“A slide is placed #{rand(3…15)} cm to the left of a lens.”
Additionally you could do something like
questions = [ “a slide is placed #{a=rand(3…15), a} cm to the left of a
lens.”
a could then be passed to some function.
questions =
“A slide is placed #{rand(3…15)} cm to the left of a lens.”
StefaN
Tried that…
I have the following…
questions = [
“A slide is placed #{u = rand(3…15)} cm to the left of a lens. The
image is in perfect focus on the screen #{v = rand(1…4)} m to the right
of the lens. What is the power of the lens?”,
“A slide is placed #{u = rand(3…15)} cm to the left of a +#{p =
rand(3…15)} D lens. At what distance will the screen need to be placed
to the right of the lens to have the image be in focus?”,
“An image is in focus #{v = rand(3…15)} m to the right of a +#{p =
rand(3…15)} D lens. At what distance (in cm) to the left of the lens
is the slide placed?”
puts questions[0]
puts u
puts v
I can’t figure out why but the values of u and v returned are always the
values of the first instance of #{} in the second and third question.
rand(3…15)} D lens. At what distance (in cm) to the left of the lens
is the slide placed?"
puts questions[0]
puts u
puts v
I can’t figure out why but the values of u and v returned are always the
values of the first instance of #{} in the second and third question.
They are the values of the last assignment to u and v.
All the assignments are done when the questions array is defined,
so u and v are reassigned by the second and third question.
Additionally you could do something like
questions = [ “a slide is placed #{a=rand(3…15), a} cm to the left of a
lens.”
a could then be passed to some function.
If you have not been using Ruby for a long time, you should probably
test your answers before posting.
Ruby is not C, and there is not a “comma operator” like C. What you have
written will assign a two-element array to a. It’s the same as:
a = [rand(3…15), a]
Because ‘a’ has not been assigned at this point, the value you get is
a = [rand(3…15), nil]
However, you could simply write:
questions = [ “a slide is placed #{a=rand(3…15)} cm to the left of a
lens.” ]