dynamic function defining

Discussion in 'Ruby' started by Reacher, Jul 2, 2007.

  1. Reacher

    Reacher Guest

    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?
     
    Reacher, Jul 2, 2007
    #1
    1. Advertising

  2. On Jul 2, 3:20 pm, Reacher <> 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.
     
    david karapetyan, Jul 3, 2007
    #2
    1. Advertising

  3. Reacher

    Reacher Guest

    On Jul 2, 10:50 pm, david karapetyan <> 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'
     
    Reacher, Jul 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    372
    Martin Honnen
    May 31, 2005
  2. johny smith
    Replies:
    8
    Views:
    443
    Peter Koch Larsen
    Jul 2, 2004
  3. Xah Lee
    Replies:
    5
    Views:
    304
    Tad McClellan
    Jan 18, 2005
  4. Sur
    Replies:
    4
    Views:
    224
  5. Anukul Singhal
    Replies:
    1
    Views:
    100
    Peña, Botp
    May 20, 2008
Loading...

Share This Page