tags:

views:

116

answers:

2

I want to allow people using my program to build up what amounts to several if conditions, which they will select from drop down lists.

First drop down : object
Second drop down : attribute
Third drop down : sign
Fourth drop down : amount

So the user's selection might look something like this;

person_a
age
>
18

Is it possible to build up an if condition in a string and then execute it?

+2  A: 

You might want to take a look at Ruby's eval method. It allows you to evaluate a Ruby expression stored in a string, so you could build up an if statement from your menus and evaluate it using eval.

eval can be dangerous, especially when dealing with user input, but if you're careful it's one method you might be able to use.

mipadi
I've done this in c# (despite that it's not a dynamic language).
Arnis L.
+8  A: 

Assuming you have a method find_person, which takes the person's name and returns the person object, you can do

find_person(person_name).send(attribute).send(sign, amount)
sepp2k
This is a much better/more rubyish way than evaling a string. We've got a rich object system and message passing for a reason.
Ben Hughes
No offense meant to mipadi, but this solution is so much better.
Chuck