Class Methods

Discussion in 'Ruby' started by Adelle Hartley, Mar 19, 2005.

  1. Hi all,

    I have a routine that creates classes based on data retrieved from a
    database.

    It goes something like this:

    unless Object.const_defined?(class_name)
    Object.const_set(class_name, Class.new(MyBaseClass) do

    # define a method, that will apply to all
    # generated classes.
    def foo() 'foo' end

    end)
    end
    end


    Is there a way of making foo a class method without resorting to eval?

    Adelle.
    Adelle Hartley, Mar 19, 2005
    #1
    1. Advertising

  2. Adelle Hartley wrote:

    > I have a routine that creates classes based on data retrieved from a
    > database.
    >
    > It goes something like this:
    >
    > unless Object.const_defined?(class_name)
    > Object.const_set(class_name, Class.new(MyBaseClass) do
    >
    > # define a method, that will apply to all
    > # generated classes.
    > def foo() 'foo' end
    >
    > end)
    > end
    > end
    >
    > Is there a way of making foo a class method without resorting to eval?


    def self.foo() 'foo' end
    Florian Gross, Mar 19, 2005
    #2
    1. Advertising

  3. Hi Florian,

    > > Is there a way of making foo a class method without

    > resorting to eval?
    >
    > def self.foo() 'foo' end


    Woohoo! That was easy! Thanks.

    Adelle.
    Adelle Hartley, Mar 20, 2005
    #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. E11
    Replies:
    1
    Views:
    4,716
    Thomas Weidenfeller
    Oct 12, 2005
  2. John M. Gabriele
    Replies:
    18
    Views:
    1,144
    Steven Bethard
    Feb 18, 2005
  3. Oltmans
    Replies:
    6
    Views:
    327
    Terry Reedy
    Mar 11, 2009
  4. DG
    Replies:
    3
    Views:
    326
    Terry Reedy
    Jul 22, 2009
  5. Kenneth McDonald
    Replies:
    5
    Views:
    301
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page