# Create object by reflection

**URL:** <https://www.ruby-forum.com/t/create-object-by-reflection/209765>\
**Category:** Ruby\
**Created:** [July 22, 2011, 5:16am UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765 "2011-07-22T05:16:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [July 22, 2011, 5:16am UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/1 "2011-07-22T05:16:58Z")

</div>

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

And how about to

build “int0”,“Person”,[“name”,]

Since the Person is:

class Person  
attr\_accessor :name, :surname  
def initialize(name,surname)  
@name = name  
@surname = surname  
end  
end

–  
leandro moreira - leandromoreira.com.br

---

<div class="post-metadata">

**Author:** ![leandromaioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@leandromaioral](https://www.ruby-forum.com/u/leandromaioral)\
**Post date:** [July 22, 2011, 6:13am UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/2 "2011-07-22T06:13:48Z")

</div>

xxx

---

<div class="post-metadata">

**Author:** ![leandromaioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@leandromaioral](https://www.ruby-forum.com/u/leandromaioral)\
**Post date:** [July 22, 2011, 10:10am UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/3 "2011-07-22T10:10:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![leandromaioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@leandromaioral](https://www.ruby-forum.com/u/leandromaioral)\
**Post date:** [July 22, 2011, 2:20pm UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/4 "2011-07-22T14:20:39Z")

</div>

7stud – wrote in post #1012360:

> Brian C. wrote in post #1012354:
> 
> > 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
> 
> `initialize’: wrong number of arguments (3 for 2) (ArgumentError)
> 
> `initialize’: wrong number of arguments (1 for 2) (ArgumentError)

Show your exact code. I would guess that you are passing an array of 3  
elements for args, when your class’s initialize method takes only two.

Here’s how it works:

> > class Foo  
> > def initialize(a,b)  
> > @a, @b = a, b  
> > end  
> > end  
> > =\> nil  
> > args = [“foo”, “bar”]  
> > =\> [“foo”, “bar”]  
> > Foo.new(\*args)  
> > =\> #\<Foo:0x7f0223609470 @a=“foo”, @b=“bar”\>

---

<div class="post-metadata">

**Author:** ![leandromaioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@leandromaioral](https://www.ruby-forum.com/u/leandromaioral)\
**Post date:** [July 22, 2011, 3:38pm UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/5 "2011-07-22T15:38:52Z")

</div>

Cool, thanks you all! =D

---

<div class="post-metadata">

**Author:** ![leandromaioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@leandromaioral](https://www.ruby-forum.com/u/leandromaioral)\
**Post date:** [July 22, 2011, 10:35am UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/6 "2011-07-22T10:35:33Z")

</div>

Brian C. wrote in post #1012354:

> 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

`initialize’: wrong number of arguments (3 for 2) (ArgumentError)

`initialize’: wrong number of arguments (1 for 2) (ArgumentError)

---

<div class="post-metadata">

**Author:** ![leandromaioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@leandromaioral](https://www.ruby-forum.com/u/leandromaioral)\
**Post date:** [July 22, 2011, 10:07pm UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/7 "2011-07-22T22:07:17Z")

</div>

I solved, stupid typo.

😉

---

<div class="post-metadata">

**Author:** ![leandromaioral](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@leandromaioral](https://www.ruby-forum.com/u/leandromaioral)\
**Post date:** [July 22, 2011, 9:03pm UTC](https://www.ruby-forum.com/t/create-object-by-reflection/209765/8 "2011-07-22T21:03:09Z")

</div>

It’s not working. Suppose I have the builder and RSpec and I tried to  
use the approach but both tests failed (String fails because it can’t  
convert array to String, and second because wrong number of arguments (1  
for 2)

I’m using ruby 1.9.1(1.9.2b)

class Make  
attr\_accessor :instances  
def initialize  
@instances = {}  
end  
def build(instance,clazz,\*args)  
if args == nil  
@instances[instance] = Object.const\_get(clazz).new  
else  
@instances[instance] = Object.const\_get(clazz).new(args)  
end  
end  
end

And it’s RSpec

describe Make do  
before(:all) do  
@make = Make.new  
end  
describe “Make Instruction” do  
it “should keep an instance of String” do  
created\_object = “test”  
@make.build “instance01”, “String”,[“test”]  
@make.instances[“instance01”].should == created\_object  
end  
it “should keep an instance of Foo” do  
@make.build “instance02”, “Foo”,[“one”,“two”]  
end  
end  
end

class Foo  
attr\_accessor :one, :two  
def initialize(one,two)  
@one = one  
@two = two  
end  
end

And the ouput from RSpec is:

1. 
* * *

Failure/Error: @make.build “instance01”, “String”,[“test”]  
TypeError:  
can’t convert Array into String

# ./lib/instructions/make.rb:11:in `initialize’

# ./lib/instructions/make.rb:11:in `new’

# ./lib/instructions/make.rb:11:in `build’

# ./spec/make\_spec.rb:11:in `block (3 levels) in \<top (required)\>’
2. 
* * *

Failure/Error: def initialize(one,two)  
ArgumentError:  
wrong number of arguments (1 for 2)

# ./spec/make\_spec.rb:22:in `initialize’

# ./lib/instructions/make.rb:11:in `new’

# ./lib/instructions/make.rb:11:in `build’

# ./spec/make\_spec.rb:15:in `block (3 levels) in \<top (required)\>’
