Ruby - storing part of the code in a database

    Today I found another way how to use the "chip" of interpreted languages ​​- to create and execute program code during the program.

    You can put part of the code in the database.

    What is it for?

    I met such a task - to write online tests.
    Each test has a function - according to which the user's answers are assigned - the number of the result description,

    and for each test this function can be different. And I don’t really want to change the site program code when adding each new test. The solution I found was to store this part of the code in a database in a text type field.

    and now in the place where you want to calculate the test result, write:
    eval (@ test.function)

    function - this is the cell in the database table that describes the tests.
    at the same time, the code is simply inserted into this place of the program and executed, it doesn’t even need to pass the results of user responses as parameters, it already sees them

    True, if inside the line passed to eval to declare a local variable, the external program will not see it (by at least I didn’t see it, although everything was ok in the console), so it’s better to use instance variables of the form - @varname I

    suspect fair criticism from fans of strict OOP. But I will not bring this example in agreement with the OOP; if you want, experiment.

    Also popular now: