[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Creating arbitrary objects

Matthew Thill

4/29/2005 5:39:00 PM

I've probably overlooked something, and hopefully someone can help me
out. I want to be able to create arbitrary object from the contents of a
string.

For example, if I had the following string:

classname = "Array"

How can I create an Array object, knowing only that the variable
classname held the name of some class as a string.


9 Answers

James Gray

4/29/2005 6:05:00 PM

0

On Apr 29, 2005, at 12:38 PM, Matthew Thill wrote:

> I've probably overlooked something, and hopefully someone can help me
> out. I want to be able to create arbitrary object from the contents of
> a string.
>
> For example, if I had the following string:
>
> classname = "Array"
>
> How can I create an Array object, knowing only that the variable
> classname held the name of some class as a string.

You're looking for Object.const_get():

irb(main):003:0> class_obj = Object.const_get("Array")
=> Array
irb(main):004:0> arr = class_obj.new
=> []

Hope that helps.

James Edward Gray II



Matthew Thill

4/29/2005 6:16:00 PM

0

Yes, that is exactly what I wanted. Thank you.

James Edward Gray II wrote:
> On Apr 29, 2005, at 12:38 PM, Matthew Thill wrote:
>
>> I've probably overlooked something, and hopefully someone can help me
>> out. I want to be able to create arbitrary object from the contents of
>> a string.
>>
>> For example, if I had the following string:
>>
>> classname = "Array"
>>
>> How can I create an Array object, knowing only that the variable
>> classname held the name of some class as a string.
>
>
> You're looking for Object.const_get():
>
> irb(main):003:0> class_obj = Object.const_get("Array")
> => Array
> irb(main):004:0> arr = class_obj.new
> => []
>
> Hope that helps.
>
> James Edward Gray II
>
>


Joe Van Dyk

4/29/2005 6:25:00 PM

0

On 4/29/05, James Edward Gray II <james@grayproductions.net> wrote:
> On Apr 29, 2005, at 12:38 PM, Matthew Thill wrote:
>
> > I've probably overlooked something, and hopefully someone can help me
> > out. I want to be able to create arbitrary object from the contents of
> > a string.
> >
> > For example, if I had the following string:
> >
> > classname = "Array"
> >
> > How can I create an Array object, knowing only that the variable
> > classname held the name of some class as a string.
>
> You're looking for Object.const_get():

Don't you mean Module.const_get()?



Christopher

4/29/2005 6:36:00 PM

0

On Sat, 2005-04-30 at 03:05 +0900, James Edward Gray II wrote:
> On Apr 29, 2005, at 12:38 PM, Matthew Thill wrote:
>
> > I've probably overlooked something, and hopefully someone can help me
> > out. I want to be able to create arbitrary object from the contents of
> > a string.
> >
> > For example, if I had the following string:
> >
> > classname = "Array"
> >
> > How can I create an Array object, knowing only that the variable
> > classname held the name of some class as a string.
>
> You're looking for Object.const_get():
>
> irb(main):003:0> class_obj = Object.const_get("Array")
> => Array
> irb(main):004:0> arr = class_obj.new
> => []
>
> Hope that helps.
>
> James Edward Gray II
>
>
>

If your class is nested in a module or other class, you might want to do
something like:

irb:0> module A
irb:1> class B
irb:2> class C
irb:3> end
irb:2> end
irb:1> end
=> nil
irb:0> classname = 'A::B::C'
=> "A::B::C"
irb:0> klass = classname.split('::').inject(Class) do |sum, elem|
irb:1* sum.const_get(elem)
irb:1> end
=> A::B::C
irb:0> klass.new
=> #<A::B::C:0xb7d12640>






James Gray

4/29/2005 7:20:00 PM

0

On Apr 29, 2005, at 1:24 PM, Joe Van Dyk wrote:

> Don't you mean Module.const_get()?

Yes, my bad. Module.const_get() called on Object, is what I meant.

James Edward Gray II



Ara.T.Howard

4/29/2005 7:49:00 PM

0

Mark Hubbart

4/29/2005 7:50:00 PM

0

On 4/29/05, James Edward Gray II <james@grayproductions.net> wrote:
> On Apr 29, 2005, at 1:24 PM, Joe Van Dyk wrote:
>
> > Don't you mean Module.const_get()?
>
> Yes, my bad. Module.const_get() called on Object, is what I meant.

actually, Module#const_get, or Object.const_get, IMHO :)

Also, the OP might look at #allocate. #allocate never needs arguments,
#new might fail without arguments. Of course, allocate doesn't
initialize...

klass = "Array"
Object.const_get(klass).allocate #==> []

cheers,
Mark



Ilmari Heikkinen

4/29/2005 8:12:00 PM

0


On 29.4.2005, at 22:50, Mark Hubbart wrote:

> Also, the OP might look at #allocate. #allocate never needs arguments,
> #new might fail without arguments. Of course, allocate doesn't
> initialize...
>
> klass = "Array"
> Object.const_get(klass).allocate #==> []
>
> cheers,
> Mark
>

Whoah, how did this escape me for so long!?

#allocate

Awesome, this'll let me abuse classes in a whole new fashion
*cue evil laughter echoing from the depths of a dark forest*

Thanks a million for the insight,
Ilmari



Matthew Thill

4/29/2005 10:19:00 PM

0

Matthew Thill wrote:
> I've probably overlooked something, and hopefully someone can help me
> out. I want to be able to create arbitrary object from the contents of a
> string.
>
> For example, if I had the following string:
>
> classname = "Array"
>
> How can I create an Array object, knowing only that the variable
> classname held the name of some class as a string.
>

Thanks for the responses everyone. I have plenty of new ideas to work
with now.