instantiating one of each object type

K

Kev Jackson

Hi all,

I want to create an array of one of each class in ruby for testing
purposes in pseudo-code I kind of want...

ObjectSpace.each_object(Class) do |c|
c.send:)new) # I want to instantiate here and store the resulting
objects in an array
end

Ok so I can half do it, but I need to dynamically send the correct
arguments for each objects initialize method - any good ideas?

Kev
 
T

Trans

You gogin to have trouble instatiating without know what it is. Try
#allocate:

objs = []
ObjectSpace.each_object(Class) do |c|
objs << c.allocate
end
p objs

T.
 
R

Robert Klemme

Trans said:
You gogin to have trouble instatiating without know what it is. Try
#allocate:

objs = []
ObjectSpace.each_object(Class) do |c|
objs << c.allocate
end
p objs

There should at least be a rescue clause as not every class supports
#allocate:
TypeError: allocator undefined for Binding
from (irb):14:in `allocate'
from (irb):14
from (irb):14

Kind regards

robert
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top