Replacement for eval?

Hi!

First of all: I’m a German pupil who is just learning ruby and English
:slight_smile:

I have a xml-file in which most elements are standing for
function-/method-calls. I “convert” each element in a
Ruby-method-call(string) and add this method-call to one string.
Because I need to call these methods several times I used eval until now
to parse this string.
But someone from the German mailinglist told me that eval is slowly and
that programming with eval isn’t that good.
I think do parse the xml-file again and again must be very slow, or I’m
wrong?
So: what can I use instead of eval?

//Tobias

So: what can I use instead of eval?

Check out the send method

def call_method(obj, methodName)
obj.send(methodName)
end

call_method(myObj, ‘do_thing’)

Chris
http://clabs.org

Tobias,

i did a similar thing creating xml:Tails (a Tag Attribute Language for
Ruby and XML like Zope’s) i did not bother to keep working on it because
it was way too slow.

the best thing to do, if you don’t have to do full out evaluation of
blocks of Ruby code, is to use #send.

~transami

···

On Wed, 2002-08-28 at 08:24, Tobias Huesgen wrote:

Hi!

First of all: I’m a German pupil who is just learning ruby and English
:slight_smile:

I have a xml-file in which most elements are standing for
function-/method-calls. I “convert” each element in a
Ruby-method-call(string) and add this method-call to one string.
Because I need to call these methods several times I used eval until now
to parse this string.
But someone from the German mailinglist told me that eval is slowly and
that programming with eval isn’t that good.
I think do parse the xml-file again and again must be very slow, or I’m
wrong?
So: what can I use instead of eval?

Hi!

Chris Morris schrieb/wrote:

So: what can I use instead of eval?

Check out the send method

def call_method(obj, methodName)
obj.send(methodName)
end

call_method(myObj, ‘do_thing’)

Thank you for the tip :slight_smile:
But can you explain me how I should use it?

//Tobias

Hi!

[…]

i did a similar thing creating xml:Tails (a Tag Attribute Language for
Ruby and XML like Zope’s) i did not bother to keep working on it
because it was way too slow.

Speed is important for my application…

the best thing to do, if you don’t have to do full out evaluation of
blocks of Ruby code, is to use #send.

As far as i understood it(the English), I want to “do full out
evaluation of blocks of Ruby code”

~transami

//Tobias

But can you explain me how I should use it?

Assuming I’m understanding what you want:

D:\temp>type test.rb
class MyClass
def do_this
puts 'do_this called’
end
end

c = MyClass.new
methodNameReadFromXml = 'do_this’
c.send(methodNameReadFromXml)

D:\temp>test.rb
do_this called

… otherwise you’d have to call eval:

D:\temp>type test.rb
class MyClass
def do_this
puts 'do_this called’
end
end

c = MyClass.new
methodNameReadFromXml = 'do_this’
eval(“c.#{methodNameReadFromXml}”)

D:\temp>test.rb
do_this called

Chris
http://clabs.org

Hi!

Chris Morris schrieb/wrote:

Assuming I’m understanding what you want:
[example]

No/Yes :wink:
The problem is I have a method which must execute the methods who are
declared in the xml-file many times.
I could put the method-calls into a array to use #send, but is this
faster then eval?

//Tobias

I could put the method-calls into a array to use #send, but is this
faster then eval?

I believe so …

Chris