Dynamic Class Initialization

P

Peer Allan

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
 
E

Enrique Comba Riepenhausen

Hi Peer,

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
 
P

Peer Allan

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

Peer
 
D

Daniel Kempkens

Peer said:
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.
 
R

Rob Biedenharn

Hi 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:
NameError: wrong constant name ActiveRecord::Base
from (irb):1:in `const_get'
from (irb):1scope.const_get(name)}
=> ActiveRecord::Base

-Rob


Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
G

gz zz

Peer said:
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
 
R

Ryan Davis

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

Daniel Kempkens

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,261
Messages
2,571,308
Members
47,976
Latest member
AlanaKeech

Latest Threads

Top