# Passing the class as an argument

**URL:** <https://www.ruby-forum.com/t/passing-the-class-as-an-argument/48926>\
**Category:** Ruby\
**Created:** [November 20, 2005, 9:07pm UTC](https://www.ruby-forum.com/t/passing-the-class-as-an-argument/48926 "2005-11-20T21:07:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![damphyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7993a0/32.png) [@damphyr](https://www.ruby-forum.com/u/damphyr)\
**Post date:** [November 20, 2005, 9:07pm UTC](https://www.ruby-forum.com/t/passing-the-class-as-an-argument/48926/1 "2005-11-20T21:07:21Z")

</div>

I’m too lazy to figure it out (and a bit hangover).  
I have several collections of objects (read over an OLE API) that I  
transform to arrays of ruby objects.  
Against the DRY religion I ended up with several methods similar to the  
following:

def get\_processors col  
ar=Array.new  
count=col.Count  
1.upto(count){|i|  
ar\<\<Processor.new(col.GetAt(i))  
}  
return ar  
end

Now, this begs for the working equivalent of

def get\_collection col, \_class  
ar=Array.new  
count=col.Count  
1.upto(count){|i|  
ar\<\<\_class.new(col.GetAt(i))  
}  
return ar  
end

How do I do it?  
(I do know how do it - I think . It’s just that the hangover is not  
helping the search functions of my brain 🙂 )

i.e. this would do what I want  
def get\_collection col, \_class  
ar=Array.new  
count=col.Count  
1.upto(count){|i|  
ar\<\<instance\_eval(\_class+“.new(col.GetAt(i))”)  
}  
return ar  
end

## …but, is there another way? Cheers, V.-

[http://www.braveworld.net/riva](http://www.braveworld.net/riva)

---

<div class="post-metadata">

**Author:** ![damphyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7993a0/32.png) [@damphyr](https://www.ruby-forum.com/u/damphyr)\
**Post date:** [November 20, 2005, 9:11pm UTC](https://www.ruby-forum.com/t/passing-the-class-as-an-argument/48926/2 "2005-11-20T21:11:22Z")

</div>

Damphyr wrote:

> ```
> ar<<Processor.new(col.GetAt(i))
> ar<<_class.new(col.GetAt(i))
> ar=Array.new
> count=col.Count
> 1.upto(count){|i|
> ar<<instance_eval(_class+".new(col.GetAt(i))")
> }
> return ar
> 
> ```
> 
> end
> 
> …but, is there another way?  
> And to clarify (it’s just impossible to concentrate today ☹ ).  
> Ideally I would like to call

```
get_collection(ole_collection,Processor)

```

which looks nicer than

```
get_collection(ole_collection,"Processor")

```

## Cheers, V.-

[http://www.braveworld.net/riva](http://www.braveworld.net/riva)

---

<div class="post-metadata">

**Author:** ![damphyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7993a0/32.png) [@damphyr](https://www.ruby-forum.com/u/damphyr)\
**Post date:** [November 20, 2005, 9:27pm UTC](https://www.ruby-forum.com/t/passing-the-class-as-an-argument/48926/3 "2005-11-20T21:27:33Z")

</div>

Hi –

On Mon, 21 Nov 2005, Damphyr wrote:

> I’m too lazy to figure it out (and a bit hangover).  
> I have several collections of objects (read over an OLE API) that I transform  
> to arrays of ruby objects.  
> Against the DRY religion I ended up with several methods similar to the  
> following:
> 
> def get\_processors col  
> ar=Array.new  
> count=col.Count

Are you sure you’ve got Count as a method name? It’s possible, but  
fairly bizarre.

> count=col.Count  
> 1.upto(count){|i|  
> ar\<\<\_class.new(col.GetAt(i))  
> }  
> return ar  
> end

I guess col is 1-originating, so you’d have to adapt this, but here it  
is with a normal array:

def get\_collection(col, klass)  
col.map {|e| klass.new(e) }  
end

If klass is a string, you’d have to do a const\_get on it first:

klass = Object.const\_get(klass)

or the traditional deep version, if it’s a nested constant:

class Module  
def deep\_const\_get©  
c.split("::").inject(self) {|acc,const| acc.const\_get(const) }  
end  
end

or some variant thereof.

David

---

<div class="post-metadata">

**Author:** ![damphyr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7993a0/32.png) [@damphyr](https://www.ruby-forum.com/u/damphyr)\
**Post date:** [November 21, 2005, 1:53am UTC](https://www.ruby-forum.com/t/passing-the-class-as-an-argument/48926/4 "2005-11-21T01:53:17Z")

</div>

David A. Black wrote:

> Are you sure you’ve got Count as a method name? It’s possible, but  
> fairly bizarre.  
> Yeap, we’re not talking Ruby objects here. It’s an OLE API and it uses  
> some pretty un-ruby names.  
> So col is an an OLE object missing such nice conveniences like each and  
> collect and map.  
> Well, assumptions can kill you. The following works as is, so all this  
> was a post with no reason to exist whatsoever.

class P  
attr\_reader :c  
def initialize c  
@c=c  
end  
def to\_s  
return @c  
end  
end  
def get\_collection col,\_class  
ar=Array.new  
col.each{|e|  
ar\<\<\_class.new(e)  
}  
return ar  
end

col=[“w”,“o”,“w”]  
ps=get\_collection col,P  
puts ps

> 

w  
o  
w

## If only everything turned out so easy… V.-

[http://www.braveworld.net/riva](http://www.braveworld.net/riva)
