Where to store functions

I am writing an optics review program that has 2 types of questions.

“Static” questions are just regular questions stored in a “questions”
table along with their associated correct answers and explanations.

“Generated” questions are generated by a “method” I write. When a
generated question is picked, the method I wrote is called and it
returns an “instance” of the question, its correct answer, and its
explanation. Each time the question is called it generates a slightly
different version of the question. I plan to have hundreds of these
methods. Currently each is a separate file.

 I do not know what the best approach to storing and accessing these

files/methods is. Is the a correct rails way to do this? I would
appreciate any help, even pointing me in the right direction would be a
great help!

Thanks!

On 25 August 2012 18:40, Dave C. [email protected] wrote:

“Generated” questions are generated by a “method” I write. When a
generated question is picked, the method I wrote is called and it
returns an “instance” of the question, its correct answer, and its
explanation. Each time the question is called it generates a slightly
different version of the question. I plan to have hundreds of these
methods. Currently each is a separate file.

What do these generated methods look like? Are they very similar to
each other? Do they follow some sort of pattern? Could that be turned
into some form of configuration information?

On Saturday, 25 August 2012 12:44:00 UTC-5, Michael P. wrote:

What do these generated methods look like? Are they very similar to
each other? Do they follow some sort of pattern? Could that be turned
into some form of configuration information?

I think I would rather store the information as a sprintf string in the
same table and then grep for %s and/or add an extra column on the table
that holds the type of dynamic question it is so that you can have a
single
method that transforms itself based on that column and then just does
something like "hello %s, how are you?" % 'Jordon' but you can do that
for complete phrases and such. This makes it so you have a single
method
for both dynamic and “static”… Or you could do that with I18n but I
still prefer to keep those in the database and cache them into memory as
I
pull them out of the database.

They are all very different, although all return a similar hash of
arguments.

Here is an example…

def vergence_v
u = (rand(2…6)).round(2)
u_vergence =((1/(u*0.01)).round(2))
p = rand((u_vergence + 0.1) …(u_vergence + 0.7)).round(2)
v_vergence = (p - u_vergence).round(2)
v = (1/v_vergence).round(2)

question = “A slide is placed #{u} cm to the left of a +#{p} D lens.
At what distance (in meters) will the screen need to be placed to the
right of the lens to have the image be in perfect focus?”

answer = “Answer = #{v} meters”

anno = "Solution:

Use the formula U + P = V.

U = Vergence of light rays from the object entering the lens
P = Power of the lens in diopters
V = Vergence of light rays leaving the lens and forming the image

where u = #{u} cm
      P = #{p} D

(1) Solve for V
    V = U + P

(2) Solve for U
    Since Vergence = 1/distance in meters, we need to convert all 

units from centimeters to meters.

         u = #{u} cm = #{u} X 0.01 = #{((u)*0.01).round(2)} m

    U is negative because it is a real object and light rays DIVERGE 

from REAL objects

         U = -1.0/#{(u*0.01).round(2)}m = 

#{(-1.0/(u*0.01)).round(2)}D

(3) Solve for V

        V = U + P = #{(-1.0/(u*0.01)).round(2)}D + #{p}D = 

#{((-1.0/(u*0.01)) + p).round(2)}D

(4) Solve for v

        v = 1/V = #{(1/v_vergence).round(2)}cm"

formatted = {“question_1” => question, “answer_1” => answer, “anno_1”
=> anno}

end

They are all very different, although all return a similar hash of
arguments.

Here is an example…

def vergence_v
u = (rand(2…6)).round(2)
u_vergence =((1/(u*0.01)).round(2))
p = rand((u_vergence + 0.1) …(u_vergence + 0.7)).round(2)
v_vergence = (p - u_vergence).round(2)
v = (1/v_vergence).round(2)

question = “A slide is placed #{u} cm to the left of a +#{p} D lens.
At what distance (in meters) will the screen need to be placed to the
right of the lens to have the image be in perfect focus?”

answer = “Answer = #{v} meters”
anno = “Solution:…”

formatted = {“question_1” => question, “answer_1” => answer, “anno_1”
=> anno}

end

Could this be saved as a string in the database, and then be written to
a temp file and “required” by the model when it is needed?