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 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 B.
“Inside every large system there’s a small system trying to get out”.
- Chet Hendrickson
Wow !
Thank you.
You’re welcome.
Now go help the next person, when you know something they (yet) do not.
–
James B.
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
On Dec 26, 2006, at 6:52 PM, James B. 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 S. wrote:
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 B.
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
On Dec 26, 2006, at 11:46 PM, James B. wrote:
Foo::Bar.new.baz
Object.const_get( ‘Foo::Bar’ ).new.baz
touché… 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
digging it up. Your google-fu is more powerful than mine
-Mat