Re: Dynamic class instantiation
- From: Jason <jasonlee9@xxxxxxxxx>
- Date: Fri, 20 Jan 2006 06:23:14 +0900
unknown wrote:
>
> 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
--
Posted via http://www.ruby-forum.com/.
.
- Follow-Ups:
- Re: Dynamic class instantiation
- From: Eric Hodel
- Re: Dynamic class instantiation
- References:
- Dynamic class instantiation
- From: Jason
- Re: Dynamic class instantiation
- From: ara . t . howard
- Dynamic class instantiation
- Prev by Date: spinbox bombs
- Next by Date: When to Use Protected vs. Private?
- Previous by thread: Re: Dynamic class instantiation
- Next by thread: Re: Dynamic class instantiation
- Index(es):
Relevant Pages
|