[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dynamic Class Initialization

Peer Allan

6/15/2007 2:35:00 PM

Hi all,
I know this can be done, but I can't find out how to do it. I have a
string with the name of the class that I want to initialize, but how do
I do it?

This is what I tried,

class = "Product"
product = class.new

Obviously this does not work or I wouldn't be asking :) So, how do I do
this?

Peer

--
Posted via http://www.ruby-....

8 Answers

Enrique Comba Riepenhausen

6/15/2007 2:42:00 PM

0

Hi Peer,

On Friday, June 15, 2007, at 04:36PM, "Peer Allan" <peer@canadadrugs.com> wrote:
>Hi all,
>I know this can be done, but I can't find out how to do it. I have a
>string with the name of the class that I want to initialize, but how do
>I do it?
>
>This is what I tried,
>
>class = "Product"
>product = class.new
>
>Obviously this does not work or I wouldn't be asking :) So, how do I do
>this?
>
>Peer

Try this:

clazz = Object.const_get("Product")
product = clazz.new

The word class is a reserved word so yu should use something different...

Cheers,

Enrique

Farrel Lifson

6/15/2007 2:46:00 PM

0

On 15/06/07, Peer Allan <peer@canadadrugs.com> wrote:
> class = "Product"
> product = class.new

class = "Product"
product = Object.const_get(class).new

Farrel

Peer Allan

6/15/2007 2:55:00 PM

0

Thanks all. I knew this couldn't be that difficult. I must have been
searching for the wrong thing.

Peer

--
Posted via http://www.ruby-....

Daniel Kempkens

6/15/2007 3:09:00 PM

0

Peer Allan schrieb:
> Hi all,
> I know this can be done, but I can't find out how to do it. I have a
> string with the name of the class that I want to initialize, but how do
> I do it?
>
> This is what I tried,
>
> class = "Product"
> product = class.new
>
> Obviously this does not work or I wouldn't be asking :) So, how do I do
> this?
>
> Peer
>
I suggest you use this method (out of Rails):

def constantize(camel_cased_word)
unless /^(::)?([A-Z]\w*)(::[A-Z]\w*)*$/ =~ camel_cased_word
raise NameError, "#{camel_cased_word.inspect} is not a valid
constant name!"
end

camel_cased_word = "::#{camel_cased_word}" unless $1
Object.module_eval(camel_cased_word, __FILE__, __LINE__)
end

It's a bit more powerful than the built-in Ruby Version.

Rob Biedenharn

6/15/2007 3:14:00 PM

0


On Jun 15, 2007, at 10:41 AM, Enrique Comba Riepenhausen wrote:

> Hi Peer,
>
> On Friday, June 15, 2007, at 04:36PM, "Peer Allan"
> <peer@canadadrugs.com> wrote:
>> Hi all,
>> I know this can be done, but I can't find out how to do it. I have a
>> string with the name of the class that I want to initialize, but
>> how do
>> I do it?
>>
>> This is what I tried,
>>
>> class = "Product"
>> product = class.new
>>
>> Obviously this does not work or I wouldn't be asking :) So, how
>> do I do
>> this?
>>
>> Peer
>
> Try this:
>
> clazz = Object.const_get("Product")
> product = clazz.new
>
> The word class is a reserved word so yu should use something
> different...
>
> Cheers,
>
> Enrique

# from Jim Weirich (based on email correspondence)
def constantize(camel_cased_word)
camel_cased_word.
sub(/^::/,'').
split("::").
inject(Object) { |scope, name| scope.const_get(name) }
end

Actually, Jim answered this question twice, once directly and once in
a post to our local users group with nearly identical code, but
clearly typed in ad hoc both times.

If you need to deal with something like ActiveRecord::Base, the
simple form doesn't work:

>> Object.const_get "ActiveRecord::Base"
NameError: wrong constant name ActiveRecord::Base
from (irb):1:in `const_get'
from (irb):1
>> "ActiveRecord::Base".split('::').inject(Object) {|scope,name|
scope.const_get(name)}
=> ActiveRecord::Base

-Rob


Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com




gz zz

6/16/2007 5:13:00 AM

0

Peer Allan wrote:
> Hi all,
> I know this can be done, but I can't find out how to do it. I have a
> string with the name of the class that I want to initialize, but how do
> I do it?
>
> This is what I tried,
>
> class = "Product"
> product = class.new
>
> Obviously this does not work or I wouldn't be asking :) So, how do I do
> this?
>
> Peer
hello,may this can work :)

product=eval("Product").new

--
Posted via http://www.ruby-....

Ryan Davis

6/16/2007 3:28:00 PM

0


On Jun 15, 2007, at 08:15 , Daniel Kempkens wrote:

> I suggest you use this method (out of Rails):

I suggest you don't. It is more wrong than right. Example:

% irb
>> x = []
=> []
>> ObjectSpace.each_object(Module) { |k| x << k if k.name =~ /^[a-z]/ }
=> 341
>> x
=> [fatal]

Using module_eval is dumb too. Use this instead:

full_name.split(/::/).inject(Object) { |klass, name| klass.const_get
name }


Daniel Kempkens

6/16/2007 3:35:00 PM

0

Ryan Davis schrieb:
>
> On Jun 15, 2007, at 08:15 , Daniel Kempkens wrote:
>
>> I suggest you use this method (out of Rails):
>
> I suggest you don't. It is more wrong than right. Example:
>
> % irb
> >> x = []
> => []
> >> ObjectSpace.each_object(Module) { |k| x << k if k.name =~ /^[a-z]/ }
> => 341
> >> x
> => [fatal]
>
> Using module_eval is dumb too. Use this instead:
>
> full_name.split(/::/).inject(Object) { |klass, name| klass.const_get name }
>
>
Wow, okay. I didn't know about that.
It's never too late to learn something new :)