Dynamic class instantiation

J

Jason

Hi all,

I have a situation where I need to dynamically execute an object from a
String name. I read in pickaxe that as long as a capitalize the class
name in the string, Ruby will see it as a Constant and thusly allow me
to use it.

For example:

module Test

class MyClass
def do_something
puts "Something done."
end
end

end

(caller code)

cls = "Test::MyClass"
require cls
obj = cls.new
obj.do_something

------

When I execute the above caller code, I get an error saying

undefined method `new' for "Test::MyClass":String (NoMethodError)

So, obviously, Ruby is telling me that there is no new method for the
String "Test::MyClass". I'm trying to instantiate the class this way as
I have specific methods that return object besides String, so I'm
guessing eval(cls) isn't appropriate for me. Also, I checked the
archives on the list with no luck.

Any suggestions?

Thx,

- jason
 
A

ara.t.howard

Hi all,

I have a situation where I need to dynamically execute an object from a
String name. I read in pickaxe that as long as a capitalize the class
name in the string, Ruby will see it as a Constant and thusly allow me
to use it.

For example:

module Test

class MyClass
def do_something
puts "Something done."
end
end

end

(caller code)

cls = "Test::MyClass"
require cls
obj = cls.new
obj.do_something

------

When I execute the above caller code, I get an error saying

undefined method `new' for "Test::MyClass":String (NoMethodError)

So, obviously, Ruby is telling me that there is no new method for the
String "Test::MyClass". I'm trying to instantiate the class this way as
I have specific methods that return object besides String, so I'm
guessing eval(cls) isn't appropriate for me. Also, I checked the
archives on the list with no luck.

Any suggestions?

Thx,


def klass_stamp(hierachy, *a, &b)
ancestors = hierachy.split(%r/::/)
parent = Object
while((child = ancestors.shift))
klass = parent.const_get child
parent = klass
end
klass::new(*a, &b)
end


class A; class B;end; end

klass_stamp 'A::B'

hth.

-a
 
J

Jason

unknown said:
def klass_stamp(hierachy, *a, &b)
ancestors = hierachy.split(%r/::/)
parent = Object
while((child = ancestors.shift))
klass = parent.const_get child
parent = klass
end
klass::new(*a, &b)
end


class A; class B;end; end

klass_stamp 'A::B'

hth.

-a

Actually, I just found this, which is a little simpler, but looks like
it does the same :)

mod = Module
name.split(/::/).each {|m| mod = mod.const_get(m) }
m = mod.new

# Call my method
m.do_something

Thx,

- jason
 
E

Eric Hodel

Actually, I just found this, which is a little simpler, but looks like
it does the same :)

mod = Module
name.split(/::/).each {|m| mod = mod.const_get(m) }
m = mod.new

def Object.path2class(path) # matches a C method that is not exported
into Ruby
return path.split('::').inject(Object) { |k,n| k.const_get n }
end

m = Object.path2class('Test::MyClass').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

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top