Access returned value in a string?

Can I access the returned value in a string?

eg
questions = [
“A slide is placed #{rand(3…15)} cm to the left of a lens.”,

]

puts questions[0]

A slide is placed 12 cm to the left of a lens.

I need to pass 12 to a function. Do I have to extract it from the
string or is it stored somewhere I can access it?

Why don’t you set a variable as
a = rand(3…15)
Then you can use a in the string and outside of it.

StefaN

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

Stefan Codrescu wrote in post #1072788:

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.

Am 19.08.2012 02:17, schrieb Dave C.:

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.

Am 19.08.2012 07:34, schrieb Wybo D.:

Start by doing:
u,v,p=nil

How would that help???

Start by doing:
u,v,p=nil


Wybo

On 2012-08-19 09:57, [email protected] wrote:

Am 19.08.2012 07:34, schrieb Wybo D.:

Start by doing:
u,v,p=nil

How would that help???

Sorry, I misunderstood the question - your answer was a lot better!

Stefan Codrescu wrote in post #1072788:

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.” ]

questions = [
[“a slide is placed %d cm to the left of a lens.” ,rand(3…15)]
]

questions[0][0] % questions[0][1]

this solv all your problems