[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Object creation with a "variable classname"

Cédric H.

12/26/2006 11:37:00 PM

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

15 Answers

James Britt

12/26/2006 11:52:00 PM

0

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

Cédric H.

12/26/2006 11:59:00 PM

0


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.

James Britt

12/27/2006 3:23:00 AM

0


>
> Wow !
> Thank you.
>

You're welcome.

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


--
James Britt

http://www.... - Hacking in the Desert
http://www.artima.c... - The Journal By & For Rubyists
http://beginni... - Beginning Ruby: The Online Book
http://www.jame... - Playing with Better Toys

Mat Schaffer

12/27/2006 4:14:00 AM

0

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

James Britt

12/27/2006 4:46:00 AM

0

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



--
James Britt

http://web2.0val... - We're the Dot in Web 2.0
http://www.... - Hacking in the Desert
http://www.jame... - Playing with Better Toys

Mat Schaffer

12/27/2006 2:43:00 PM

0


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é... I think :)

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

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

Joe Cool

7/22/2010 11:46:00 PM

0

On Jul 22, 6:58 pm, President Soetoro
<osama.obabma.by.anyn...@gmail.com> wrote:
> On Jul 22, 5:37 pm, Joe Cool <joecool1...@live.com> wrote:
>
>
>
>
>
> > On Jul 22, 6:25 pm, President Soetoro
>
> > <osama.obabma.by.anyn...@gmail.com> wrote:
> > > The "Fruit of the Loon" says....
>
> > > Fuck 10% unemployment
> > > Fuck the millions of unemployed Americans
> > > Fuck the Gulf cleanup
>
> > > Fuck America
>
> > > I'm going to Disney World!
>
> > > Yep, vacation last week, rock concert, golf, DAMN, the man is busy!
>
> > He is nowhere near on track to be the current record holder, George W
> > Bush.
>
> >http://www.cbsnews.com/stories/2008/03/11/politics/uwire/m.......
>
> It was one hell of a lot cheaper for him to go there than the cost of
> the o[BEEP]a trips...

What the fuck are you babbling abpout?

> also, tell me one time when Bush had a separate
> plane fly the First Bitch?

Michelle does not always fly on a separate plane, asswipe.

The PHANTOM

7/23/2010 12:19:00 AM

0

On Jul 22, 6:45 pm, Joe Cool <joecool1...@live.com> wrote:
> On Jul 22, 6:58 pm, President Soetoro
>
>
>
>
>
> <osama.obabma.by.anyn...@gmail.com> wrote:
> > On Jul 22, 5:37 pm, Joe Cool <joecool1...@live.com> wrote:
>
> > > On Jul 22, 6:25 pm, President Soetoro
>
> > > <osama.obabma.by.anyn...@gmail.com> wrote:
> > > > The "Fruit of the Loon" says....
>
> > > > Fuck 10% unemployment
> > > > Fuck the millions of unemployed Americans
> > > > Fuck the Gulf cleanup
>
> > > > Fuck America
>
> > > > I'm going to Disney World!
>
> > > > Yep, vacation last week, rock concert, golf, DAMN, the man is busy!
>
> > > He is nowhere near on track to be the current record holder, George W
> > > Bush.
>
> > >http://www.cbsnews.com/stories/2008/03/11/politics/uwire/m........
>
> > It was one hell of a lot cheaper for him to go there than the cost of
> > the o[BEEP]a trips...
>
> What the fuck are you babbling abpout?
>
> > also, tell me one time when Bush had a separate
> > plane fly the First Bitch?
>
> Michelle does not always fly on a separate plane, asswipe.- Hide quoted text -
>
> - Show quoted text -

How about the ugly assed dog?? Not Michelle. The other mutt.

ayatollah obama

7/23/2010 12:21:00 AM

0

On Jul 22, 6:45 pm, Joe Cool <joecool1...@live.com> wrote:
> On Jul 22, 6:58 pm, President Soetoro
>
>
>
>
>
> <osama.obabma.by.anyn...@gmail.com> wrote:
> > On Jul 22, 5:37 pm, Joe Cool <joecool1...@live.com> wrote:
>
> > > On Jul 22, 6:25 pm, President Soetoro
>
> > > <osama.obabma.by.anyn...@gmail.com> wrote:
> > > > The "Fruit of the Loon" says....
>
> > > > Fuck 10% unemployment
> > > > Fuck the millions of unemployed Americans
> > > > Fuck the Gulf cleanup
>
> > > > Fuck America
>
> > > > I'm going to Disney World!
>
> > > > Yep, vacation last week, rock concert, golf, DAMN, the man is busy!
>
> > > He is nowhere near on track to be the current record holder, George W
> > > Bush.
>
> > >http://www.cbsnews.com/stories/2008/03/11/politics/uwire/m........
>
> > It was one hell of a lot cheaper for him to go there than the cost of
> > the o[BEEP]a trips...
>
> What the fuck are you babbling abpout?
>
> > also, tell me one time when Bush had a separate
> > plane fly the First Bitch?
>
> Michelle does not always fly on a separate plane, asswipe.

You are more fucking stupid than a fucking rock.... "bitch" is a
fucking dog!!!

Your "Fruit of the Loon" put a carbon paw print the size of his home
Country Kenya to fly the fucking mutt up for his brat kids!

Joe Cool

7/23/2010 12:40:00 AM

0

On Jul 22, 8:19 pm, The PHANTOM <hoofhearte...@yahoo.com> wrote:
> On Jul 22, 6:45 pm, Joe Cool <joecool1...@live.com> wrote:
>
>
>
>
>
> > On Jul 22, 6:58 pm, President Soetoro
>
> > <osama.obabma.by.anyn...@gmail.com> wrote:
> > > On Jul 22, 5:37 pm, Joe Cool <joecool1...@live.com> wrote:
>
> > > > On Jul 22, 6:25 pm, President Soetoro
>
> > > > <osama.obabma.by.anyn...@gmail.com> wrote:
> > > > > The "Fruit of the Loon" says....
>
> > > > > Fuck 10% unemployment
> > > > > Fuck the millions of unemployed Americans
> > > > > Fuck the Gulf cleanup
>
> > > > > Fuck America
>
> > > > > I'm going to Disney World!
>
> > > > > Yep, vacation last week, rock concert, golf, DAMN, the man is busy!
>
> > > > He is nowhere near on track to be the current record holder, George W
> > > > Bush.
>
> > > >http://www.cbsnews.com/stories/2008/03/11/politics/uwire/m........
>
> > > It was one hell of a lot cheaper for him to go there than the cost of
> > > the o[BEEP]a trips...
>
> > What the fuck are you babbling abpout?
>
> > > also, tell me one time when Bush had a separate
> > > plane fly the First Bitch?
>
> > Michelle does not always fly on a separate plane, asswipe.- Hide quoted text -
>
> > - Show quoted text -
>
> How about the ugly assed dog?? Not Michelle. The other mutt.

And the other "ugly assed dog" as you so indelicately put it, doesn't
fly on a separate plane always either, you pathic asshole.