From: Brian Candler Date: 2011-07-22T17:10:53+09:00 Subject: Re: create object by reflection leandro moreira wrote in post #1012318: > My doubt is how create objects by reflection, when they need more than > one argument. Given that I need to create them based on array. > > > def build(instance,clazz,args) > if args == nil > @instances[instance] = Object.const_get(clazz).new > else > @instances[instance] = Object.const_get(clazz).new(args[0]) > end > end If args is an array: def build(instance,clazz,args) @instances[instance] = Object.const_get(clazz).new(*args) end Or if you want to call 'build' with a variable number of args: def build(instance,clazz,*args) @instances[instance] = Object.const_get(clazz).new(*args) end -- Posted via http://www.ruby-forum.com/.