Dynamic method call

I’m trying, for the purpose of making something similar to a grammar, to
do
this

if_a_condition_is_true(condition, “method_name(arg1, arg2)”)

where

def method_name(arg1, arg2)
#do something
end

i’ve tried many way to implement

def if_condition_is_true(condition , action)
#such as
eval(action)
#or
call self.method(action)

end

but noone worked, i’ve trying googling a bit but no chances. Have you
got an
idea on how to call dynamically a method, with his arguments?

Thanks a lot folks

Andrea M.

http://www.superandrew.it

A clever person solves a problem.
A wise person avoids it.

Einstein

On 8/1/07, Andrea M. [email protected] wrote:

#do something
end

http://www.superandrew.it

A clever person solves a problem.
A wise person avoids it.

Einstein

To call an arbitrary method on an object use send

object.send( :method_name_as_symbol [, arg1, arg2 …] )

You can put that in an if statement etc or use

object.send( :method ) if some_condition_is_true

HTH
Daniel

end

but noone worked, i’ve trying googling a bit but no chances. Have
you got an
idea on how to call dynamically a method, with his arguments?

Hi,
I’m not sure of why you need to implement it like this, but that’s up
to you.
This works for me:

def if_condition_is_true(condition , action)
eval(action) if condition
end

def method_name(arg1, arg2)
puts arg1+ arg2
end

if_condition_is_true(true, “method_name(2, 3)”)

Cheers,
Dave

On 8/1/07, Andrea M. [email protected] wrote:

Thank you very much guys, it worked. I was doing this for the purpose of
create a scripting language in my lang for testers to easy developing of use
case testing with watir.
For english people is probably easier to read

you really think that’s easier to read than:

method_name(2,3) if condition

???

Thank you very much guys, it worked. I was doing this for the purpose of
create a scripting language in my lang for testers to easy developing of
use
case testing with watir.
For english people is probably easier to read

the problem is that method_name is a static call, i need to make it
dynamic
and choosed by the user/programmer, so at least he should type
eval(“method_name(arg1,arg2)”) if condition

but my aim was just to encapsulate this in a more comprehensible grammar
like

if_it_happens_that(is_true, “do_something(arg1,arg2)”)

in another language that english it sound more different using english
word
“like if, then, end, do”

i know it could sound weird, but this was my need, and i solved it with
eval
(of course the internal logic is far more complex)

2007/8/1, Gregory B. [email protected]:

method_name(2,3) if condition

???

Andrea M.

http://www.superandrew.it

A clever person solves a problem.
A wise person avoids it.

Einstein

On 8/1/07, Andrea M. [email protected] wrote:

“like if, then, end, do”

i know it could sound weird, but this was my need, and i solved it with eval
(of course the internal logic is far more complex)

I still don’t quite understand the problem you’re tring to solve, but
consider using send(:method,arg1,arg2) instead of eval(). You’re
begging for a mess using eval() to allow end users to input stuff.

Le jeudi 02 août 2007 à 04:27 +0900, Andrea M. a écrit :

“like if, then, end, do”

i know it could sound weird, but this was my need, and i solved it with eval
(of course the internal logic is far more complex)

Do you want something like this :

def sendif( args, &bloc )
if args[:if]
send args[:then].shift, *args[:then], &bloc
else
send args[:else].shift, *args[:else], &bloc if args[:else]
end
end

sendif :if => 1 < 2, :then => [:p, “gagne”], :else => [:puts, “perdu”]

?

Ok, Gregory, now i understand that i didn’t explain very well. The only
part
in wich the user (let’s say a VERY junior programmer) is the scripting
interface i provide for him, so he will write
if_is_true(a_condition, “do_something(arg1,arg2)”)

tha internal implementation is my matter, but if you think eval is bad
(i
probably understand why), the solution with

send(:method,arg1,arg2)

is probably good, but how can i allow a user to input the method name
with
his arguments? Anyway, i’m going to try.

2007/8/1, Gregory B. [email protected]:

if_it_happens_that(is_true, “do_something(arg1,arg2)”)
consider using send(:method,arg1,arg2) instead of eval(). You’re
begging for a mess using eval() to allow end users to input stuff.

Andrea M.

http://www.superandrew.it

A clever person solves a problem.
A wise person avoids it.

Einstein

On Aug 2, 2007, at 2:27 AM, Andrea M. wrote:

send(:method,arg1,arg2)

is probably good, but how can i allow a user to input the method
name with
his arguments? Anyway, i’m going to try.

Have you considered:

if_is_true(condition) { do_something(…) }

?

That doesn’t require eval() or send().

James Edward G. II

Alle giovedì 2 agosto 2007, Todd B. ha scritto:

end

@operation.call ; # call whatever method was set.

Todd

If the content of the tool variable are exactly the method names, then
you
don’t need the case statement:

@operation = self.method(tool)
@operation.call

Stefano

This may be slightly different, but I am calling a method dynamically
this way:

case tool
when “drill” then
@operation = self.method(:drill) ;
when “cut” then
@operation = self.method(:cut) ;
when “bend” then
@operation = self.method(:bend) ;
end

@operation.call ; # call whatever method was set.

Todd

Hi –

On Thu, 2 Aug 2007, Todd B. wrote:

end

@operation.call ; # call whatever method was set.

Unless I’m missing a subtlety, you could also do that as the somewhat
more concise:

send(tool) if tool

David

On Aug 02, 2007, at 09:16 , [email protected] wrote:

send(tool) if tool

I would (personally) take it a step farther:

send( tool ) if tool && respond_to? tool

or something similar.

On 8/2/07, James Edward G. II [email protected] wrote:

probably understand why), the solution with

?

That doesn’t require eval() or send().

and

do_something(…) if condition

doesn’t require anything. I’m still missing what value this
if_is_true stuff is adding.

unknown wrote:

Unless I’m missing a subtlety, you could also do that as the somewhat
more concise:

send(tool) if tool

David

Thanks for the shortcuts - I didn’t know either of these techniques
could be done. In my example, I do other things inside each WHEN
statement to set up the call, but I condensed my example to post.

Thanks! Todd

From: Gregory B. [mailto:[email protected]]

do_something(…) if condition

doesn’t require anything. I’m still missing what value this

if_is_true stuff is adding.

i think he’s asking for boolean obj so he can chain conditions.
soemthing like,

cond.if_true(foo).bar.blah.if_true.baz.light

which is

if cond
if x = foo.bar.blah
x.baz.light
end
end

kind regards -botp

On 8/2/07, Peña, Botp [email protected] wrote:

which is

if cond
if x = foo.bar.blah
x.baz.light
end
end

Sure, that could be helpful, but I’m not sure how you came up with
that based on what was asked.

For what you’ve shown there, I’ve often longed for a Null object

something like

class Null < BasicObject
def self.method_missing(id,*args,&block)
self
end
end

module Kernel
def if_true(condition)
condition ? self : Null
end
end

Note that I didn’t even remotely test the above (though I have
implemented this before for fun), but that hopefully the idea is
preserved.

Hi –

On Wed, 1 Aug 2007, Andrea M. wrote:

Thank you very much guys, it worked. I was doing this for the purpose of
create a scripting language in my lang for testers to easy developing of use
case testing with watir.
For english people is probably easier to read

Maybe, maybe not… I would tend to say:

if 5 is greater than 3

rather than

if this condition is true: 5 is greater than 3

But of course it depends on the wider context you’re developing.

David

On 8/1/07, Andrea M. [email protected] wrote:

Thank you very much guys, it worked. I was doing this for the purpose of
create a scripting language in my lang for testers to easy developing of use
case testing with watir.
For english people is probably easier to read.

Andrea,

Hmm. A ‘scripting language for testers’ sounds like the RSpec
DSL[0]. Your cases should map to RSpec contexts very nicely.

Regards,

Paul.

[0] http://rspec.rubyforge.org/index.html