Object creation with a "variable classname"

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
:wink: ). 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 :slight_smile:

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 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.

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

路路路

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

Wow !
Thank you.

You're welcome.

Now go help the next person, when you know something they (yet) do not.

路路路

--
James Britt

http://www.rubyaz.org - Hacking in the Desert
Ruby Code & Style - The Journal By & For Rubyists
http://beginningruby.com - Beginning Ruby: The Online Book
http://www.jamesbritt.com - Playing with Better Toys

Mat Schaffer 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

路路路

On Dec 26, 2006, at 6:52 PM, James Britt wrote:

--
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

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茅... I think :slight_smile:

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

Yeah, that's the conversation I was talking about. Thanks for digging it up. Your google-fu is more powerful than mine :slight_smile:
-Mat

路路路

On Dec 26, 2006, at 11:46 PM, James Britt wrote: