# Question: Dynamic code execution

**URL:** <https://www.ruby-forum.com/t/question-dynamic-code-execution/185940>\
**Category:** Ruby\
**Created:** [March 29, 2010, 2:42pm UTC](https://www.ruby-forum.com/t/question-dynamic-code-execution/185940 "2010-03-29T14:42:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [March 29, 2010, 2:42pm UTC](https://www.ruby-forum.com/t/question-dynamic-code-execution/185940/1 "2010-03-29T14:42:39Z")

</div>

Hi

I’m currently writing a small game application in ruby.  
The problem is, not all of the rules are static, meaning some  
of the actions in the game are allowed to change them.  
My question is, what is the best and safest way to implement this  
feature?  
Specifically, I have database of items, which may change parts of  
the rules upon activation.  
Storing code along with those and eval()ing it at runtime feels  
kind of awkward and not really safe, but seems the most flexible  
way at a first glance.  
Has anybody ideas and/or pointers to this matter?

Thorsten

---

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [March 29, 2010, 3:14pm UTC](https://www.ruby-forum.com/t/question-dynamic-code-execution/185940/2 "2010-03-29T15:14:13Z")

</div>

Thorsten H. wrote:

> Hi
> 
> I’m currently writing a small game application in ruby.  
> The problem is, not all of the rules are static, meaning some  
> of the actions in the game are allowed to change them.  
> My question is, what is the best and safest way to implement this  
> feature?  
> Specifically, I have database of items, which may change parts of  
> the rules upon activation.

Alright… So store the rules in one or more hashes depending on what  
they are and how they can change, and refer to whatever key you need to  
read the rule (or setting) in the hash.  
That’s one way.

---

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [March 29, 2010, 7:28pm UTC](https://www.ruby-forum.com/t/question-dynamic-code-execution/185940/3 "2010-03-29T19:28:56Z")

</div>

My problem is, how to formulate the rules in ruby?  
Let me give an example:  
Lets say in an RPG context, the max skill level is 10,  
but if the player chooses to be an elf, he gets 11 as a  
max skill level in bow shooting. Additionally there  
exists something like a special feature which increases  
the max skill level by another point.  
At the moment I’m thinking about a mini DSL based on  
method\_missing, but I’m looking for alternatives.

---

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [March 29, 2010, 9:06pm UTC](https://www.ruby-forum.com/t/question-dynamic-code-execution/185940/4 "2010-03-29T21:06:39Z")

</div>

Thorsten H. wrote:

> My problem is, how to formulate the rules in ruby?  
> Let me give an example:  
> Lets say in an RPG context, the max skill level is 10,  
> but if the player chooses to be an elf, he gets 11 as a  
> max skill level in bow shooting. Additionally there  
> exists something like a special feature which increases  
> the max skill level by another point.  
> At the moment I’m thinking about a mini DSL based on  
> method\_missing, but I’m looking for alternatives.

Things which will be crucial to you here are going to be extending  
modules and classes, as well as inheritance.

class Race  
MAX\_SKILL\_LEVEL = 10  
end

class Elf \< Race  
MAX\_SKILL\_LEVEL += 1  
end

Race::MAX # =\> 10  
Elf::MAX # =\> 11

And you can also specify unique skill levels (for instance, your  
Swordmanship max skill may be 22 because of a unique sword you have, it  
doesn’t raise EVERY skill max…)

Does that help a little?

---

<div class="post-metadata">

**Author:** ![Thorsten\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/aca169/32.png) [@Thorsten\_H](https://www.ruby-forum.com/u/Thorsten_H)\
**Post date:** [March 29, 2010, 9:57pm UTC](https://www.ruby-forum.com/t/question-dynamic-code-execution/185940/5 "2010-03-29T21:57:35Z")

</div>

On Mon, 29 Mar 2010, Thorsten H. wrote:

> kind of awkward and not really safe, but seems the most flexible  
> way at a first glance.  
> Has anybody ideas and/or pointers to this matter?
> 
> Thorsten

You might consider looking at this, which talks about some dynamic game  
generation I’ve done (and if you have further questions, I’ve got more):

[http://aetherical.com/2008/4/25/classes-on-the-fly](http://aetherical.com/2008/4/25/classes-on-the-fly)
