Dynamic method call


#1

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


#2

On 8/1/07, Andrea M. removed_email_address@domain.invalid 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


#3

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


#4

On 8/1/07, Andrea M. removed_email_address@domain.invalid 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

???


#5

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


#6

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. removed_email_address@domain.invalid:

method_name(2,3) if condition

???

Andrea M.

http://www.superandrew.it

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

Einstein


#7

On 8/1/07, Andrea M. removed_email_address@domain.invalid 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.


#8

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”]

?


#9

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. removed_email_address@domain.invalid:

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


#10

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


#11

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


#12

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


#13

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


#14

On Aug 02, 2007, at 09:16 , removed_email_address@domain.invalid wrote:

send(tool) if tool

I would (personally) take it a step farther:

send( tool ) if tool && respond_to? tool

or something similar.


#15

On 8/2/07, James Edward G. II removed_email_address@domain.invalid 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.


#16

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


#17

From: Gregory B. [mailto:removed_email_address@domain.invalid]

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


#18

On 8/2/07, Peña, Botp removed_email_address@domain.invalid 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.


#19

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


#20

On 8/1/07, Andrea M. removed_email_address@domain.invalid 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