[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

dynamic function defining

Reacher

7/2/2007 10:21:00 PM

Can you define function names dynamically in Ruby? Say you have:

class Myclass
def initialize()
@name = "STR"
end

def STR1
...
end

def STR2
...
end

...

end

can the STR* functions be generated on the fly, based on the value of
@name?

2 Answers

david karapetyan

7/3/2007 2:50:00 AM

0

On Jul 2, 3:20 pm, Reacher <brandon.g.jo...@gmail.com> wrote:
> Can you define function names dynamically in Ruby? Say you have:
>
> class Myclass
> def initialize()
> @name = "STR"
> end
>
> def STR1
> ...
> end
>
> def STR2
> ...
> end
>
> ...
>
> end
>
> can the STR* functions be generated on the fly, based on the value of
> @name?

I think you should look up singleton classes and the method
"module_eval". I am not an expert on these issues but every time
dynamic programming comes up in ruby those two concepts pop up as well.

Reacher

7/3/2007 4:21:00 PM

0

On Jul 2, 10:50 pm, david karapetyan <dkarapet...@gmail.com> wrote:
>
> I think you should look up singleton classes and the method
> "module_eval". I am not an expert on these issues but every time
> dynamic programming comes up in ruby those two concepts pop up as well.

I got it to work using plain old 'eval'