[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Dynamic method call

David and Sharon Phillips

8/1/2007 6:52:00 AM

> 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?

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

22 Answers

Gregory Brown

8/1/2007 2:32:00 PM

0

On 8/1/07, Andrea Maschio <andrea.maschio@gmail.com> 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

???

Gregory Brown

8/1/2007 7:37:00 PM

0

On 8/1/07, Andrea Maschio <andrea.maschio@gmail.com> wrote:
> 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)

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.

dohzya

8/1/2007 7:49:00 PM

0

Le jeudi 02 août 2007 à 04:27 +0900, Andrea Maschio a écrit :
> 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)

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"]
---
?

--
Etienne Vallette d'Osia


James Gray

8/2/2007 12:54:00 PM

0

On Aug 2, 2007, at 2:27 AM, Andrea Maschio wrote:

> 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.

Have you considered:

if_is_true(condition) { do_something(...) }

?

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

James Edward Gray II

Todd Burch

8/2/2007 1:04:00 PM

0

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
--
Posted via http://www.ruby-....

Stefano Crocco

8/2/2007 1:11:00 PM

0

Alle giovedì 2 agosto 2007, Todd Burch ha scritto:
> 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

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

David A. Black

8/2/2007 1:16:00 PM

0

Todd Burch

8/2/2007 2:45:00 PM

0

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
--
Posted via http://www.ruby-....

Gregory Brown

8/2/2007 4:47:00 PM

0

On 8/2/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Aug 2, 2007, at 2:27 AM, Andrea Maschio wrote:
>
> > 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.
>
> Have you considered:
>
> if_is_true(condition) { do_something(...) }
>
> ?
>
> 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.

Peña, Botp

8/3/2007 2:28:00 AM

0

From: Gregory Brown [mailto:gregory.t.brown@gmail.com]
# 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