Object creation with a "variable classname"

Discussion in 'Ruby' started by cedric.ch, Dec 26, 2006.

  1. cedric.ch

    cedric.ch Guest

    Hi,

    I would to know if it is possible to do something like that:

    def create_object(class_name)
    my_object = **class_name**.new
    end

    I tried with class.new(&class_name)

    Class_name = Class.new(&class_name)
    my_object = Class_name.New

    In fact I have a single table inheritence ( yes, I'm doing a Rails app
    ;) ). According to the action the user selects , I want to create the
    apropriated object.

    My English is very bad but I'll be very happy if you could help me.

    Thanks :)
     
    cedric.ch, Dec 26, 2006
    #1
    1. Advertising

  2. cedric.ch

    James Britt Guest

    cedric.ch wrote:
    > Hi,
    >
    > I would to know if it is possible to do something like that:
    >
    > def create_object(class_name)
    > my_object = **class_name**.new
    > end
    >
    > I tried with class.new(&class_name)



    x = Object.const_get( class_name ).new



    --
    James Britt

    "Inside every large system there's a small system trying to get out".
    - Chet Hendrickson
     
    James Britt, Dec 26, 2006
    #2
    1. Advertising

  3. cedric.ch

    cedric.ch Guest

    James Britt a écrit :

    > cedric.ch wrote:
    > > Hi,
    > >
    > > I would to know if it is possible to do something like that:
    > >
    > > def create_object(class_name)
    > > my_object = **class_name**.new
    > > end
    > >
    > > I tried with class.new(&class_name)

    >
    >
    > x = Object.const_get( class_name ).new
    >
    >
    >
    > --
    > James Britt
    >
    > "Inside every large system there's a small system trying to get out".
    > - Chet Hendrickson


    Wow !
    Thank you.
     
    cedric.ch, Dec 26, 2006
    #3
  4. cedric.ch

    James Britt Guest

    James Britt, Dec 27, 2006
    #4
  5. cedric.ch

    Mat Schaffer Guest

    On Dec 26, 2006, at 6:52 PM, James Britt wrote:
    > cedric.ch wrote:
    >> Hi,
    >> I would to know if it is possible to do something like that:
    >> def create_object(class_name)
    >> my_object = **class_name**.new
    >> end
    >> I tried with class.new(&class_name)

    >
    >
    > x = Object.const_get( class_name ).new


    Does that still work if there's a module mentioned in the string with
    '::' included? I seem to remember a discussion or maybe an
    eigenclass blog entry that mentioned the problem. But maybe I'm wrong.
    -Mat
     
    Mat Schaffer, Dec 27, 2006
    #5
  6. cedric.ch

    James Britt Guest

    Mat Schaffer wrote:
    > On Dec 26, 2006, at 6:52 PM, James Britt wrote:
    >> cedric.ch wrote:
    >>> Hi,
    >>> I would to know if it is possible to do something like that:
    >>> def create_object(class_name)
    >>> my_object = **class_name**.new
    >>> end
    >>> I tried with class.new(&class_name)

    >>
    >>
    >> x = Object.const_get( class_name ).new

    >
    > Does that still work if there's a module mentioned in the string with
    > '::' included? I seem to remember a discussion or maybe an eigenclass
    > blog entry that mentioned the problem. But maybe I'm wrong.
    > -Mat


    Well, try it and see:


    module Foo
    class Bar
    def baz
    warn 'BAZ!'
    end
    end
    end

    Foo::Bar.new.baz

    Object.const_get( 'Foo::Bar' ).new.baz


    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/160468



    --
    James Britt

    http://web2.0validator.com - We're the Dot in Web 2.0
    http://www.rubyaz.org - Hacking in the Desert
    http://www.jamesbritt.com - Playing with Better Toys
     
    James Britt, Dec 27, 2006
    #6
  7. cedric.ch

    Mat Schaffer Guest

    On Dec 26, 2006, at 11:46 PM, James Britt wrote:
    > Well, try it and see:
    >
    >
    > module Foo
    > class Bar
    > def baz
    > warn 'BAZ!'
    > end
    > end
    > end
    >
    > Foo::Bar.new.baz
    >
    > Object.const_get( 'Foo::Bar' ).new.baz


    touch=E9... I think :)

    > http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/160468


    Yeah, that's the conversation I was talking about. Thanks for =20
    digging it up. Your google-fu is more powerful than mine :)
    -Mat=
     
    Mat Schaffer, Dec 27, 2006
    #7
    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. Balaji Kannan

    Which is advisable for object creation?

    Balaji Kannan, Dec 26, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    354
    Hermit Dave
    Dec 26, 2003
  2. =?Utf-8?B?QW50aG9ueSBNdW50ZXI=?=

    COM object creation fails when impersonation is done [PSC]

    =?Utf-8?B?QW50aG9ueSBNdW50ZXI=?=, May 4, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    1,978
    =?Utf-8?B?QW50aG9ueSBNdW50ZXI=?=
    May 12, 2004
  3. Harry F. Harrison

    Dynamic creation of object problem

    Harry F. Harrison, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    474
    Scott Allen
    Aug 11, 2004
  4. =?Utf-8?B?R2hhbmFTaHlhbQ==?=

    Object Creation.

    =?Utf-8?B?R2hhbmFTaHlhbQ==?=, Apr 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    373
    tdavisjr
    Apr 5, 2006
  5. jon wayne
    Replies:
    9
    Views:
    712
    Jim Langston
    Sep 22, 2005
Loading...

Share This Page