Dynamic class names

Discussion in 'Ruby' started by Martin Boese, Apr 2, 2008.

  1. Martin Boese

    Martin Boese Guest

    Is it possible to create classes with a dynamic name? Example:

    name = "Test"

    class name # this fails
    def write
    puts "TEST"
    end
    end

    puts Object::const_get(name).new.write # => "TEST"
     
    Martin Boese, Apr 2, 2008
    #1
    1. Advertising

  2. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Martin Boese wrote:
    | Is it possible to create classes with a dynamic name? Example:
    |
    | name = "Test"
    |
    | class name # this fails
    | def write
    | puts "TEST"
    | end
    | end
    |
    | puts Object::const_get(name).new.write # => "TEST"
    |
    |
    |
    Yes. Here is an overview on metaprogramming, and two ways to create
    classes dynamically:

    http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html

    - -- Phillip Gawlowski
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.8 (MingW32)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iEUEARECAAYFAkfzWNEACgkQbtAgaoJTgL9KhACfY0UzhdcpPeOYMYaOgjfUpT35
    dAwAl3hEZTSDs6+M6U0givW4nk68iGU=
    =44gD
    -----END PGP SIGNATURE-----
     
    Phillip Gawlowski, Apr 2, 2008
    #2
    1. Advertising

  3. Martin Boese

    Martin Boese Guest

    No... I mean the 'Name' of a class or module (that what you get with
    object.class.name). The doc of Class::new (ri) describes how it works:

    > You can give a class a name by assigning the class object to a constant.


    ...but still that doesn't allow me to set the initial name when _defining_ the
    class.

    I am aware that this can be done using eval with the complete class definition
    as a string - which is also not what I want.

    The reason is to avoid name clashes of a larger program. So I am thinking of
    using some central instance to assign names and address specific types of
    classes.

    Martin



    On Wednesday 02 April 2008 10:58:52 Phillip Gawlowski wrote:
    > Martin Boese wrote:
    > | Is it possible to create classes with a dynamic name? Example:
    > |
    > | name = "Test"
    > |
    > | class name # this fails
    > | def write
    > | puts "TEST"
    > | end
    > | end
    > |
    > | puts Object::const_get(name).new.write # => "TEST"
    >
    > Yes. Here is an overview on metaprogramming, and two ways to create
    > classes dynamically:
    >
    > http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html
    >
    > -- Phillip Gawlowski
     
    Martin Boese, Apr 2, 2008
    #3
  4. Martin Boese

    Robert Dober Guest

    On Wed, Apr 2, 2008 at 11:18 AM, Martin Boese <> wrote:
    > Is it possible to create classes with a dynamic name? Example:
    >
    > name = "Test"
    >
    > class name # this fails
    > def write
    > puts "TEST"
    > end
    > end
    >
    > puts Object::const_get(name).new.write # => "TEST"
    >
    >
    >

    sure

    c = Class::new {
    def a; 41 end
    }
    d = Class::new(c) {
    def a; super.succ end
    }
    d.new.a --> 42

    HTH
    Robert


    --
    http://ruby-smalltalk.blogspot.com/

    ---
    Whereof one cannot speak, thereof one must be silent.
    Ludwig Wittgenstein
     
    Robert Dober, Apr 2, 2008
    #4
  5. Martin Boese

    Martin Boese Guest

    Thanks, that's what I was looking for.

    That example of mine now works:

    name = 'Test'
    Object::const_set(name.intern, Class::new do
    def write
    puts "TEST"
    end
    end
    )
    Object::const_get(name).new.write # writes => "TEST"

    martin


    On Wednesday 02 April 2008 12:19:33 Robert Dober wrote:
    > On Wed, Apr 2, 2008 at 11:18 AM, Martin Boese <> wrote:
    > > Is it possible to create classes with a dynamic name? Example:
    > >
    > > name = "Test"
    > >
    > > class name # this fails
    > > def write
    > > puts "TEST"
    > > end
    > > end
    > >
    > > puts Object::const_get(name).new.write # => "TEST"

    >
    > sure
    >
    > c = Class::new {
    > def a; 41 end
    > }
    > d = Class::new(c) {
    > def a; super.succ end
    > }
    > d.new.a --> 42
    >
    > HTH
    > Robert
     
    Martin Boese, Apr 2, 2008
    #5
    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. Paddy McCarthy
    Replies:
    3
    Views:
    733
    Anthony J Bybell
    Sep 24, 2004
  2. Bob
    Replies:
    1
    Views:
    394
    Lucas Tam
    Jul 30, 2004
  3. E11
    Replies:
    1
    Views:
    4,840
    Thomas Weidenfeller
    Oct 12, 2005
  4. Ares Lagae
    Replies:
    8
    Views:
    455
    Ares Lagae
    Sep 24, 2004
  5. Bryan
    Replies:
    3
    Views:
    357
    Diez B. Roggisch
    Nov 13, 2009
Loading...

Share This Page