# Return a "complete" class

**URL:** <https://www.ruby-forum.com/t/return-a-complete-class/105225>\
**Category:** Ruby\
**Created:** [May 27, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/return-a-complete-class/105225 "2007-05-27T16:55:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lordbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/77aa72/32.png) [@lordbyte](https://www.ruby-forum.com/u/lordbyte)\
**Post date:** [May 27, 2007, 4:55pm UTC](https://www.ruby-forum.com/t/return-a-complete-class/105225/1 "2007-05-27T16:55:27Z")

</div>

Hi!

I have some problems with doing some kind of “my own controller” in  
Ruby when returning a complete class.

Let’s say we start with the following:

I have a …

- … model called Asset which has several attributes and methods.
- … model called Picture which inherits from Asset (class Picture \<  
Asset)
- … model called Video which inherits from Asset

So far so good. In a gallery controller, I know trough a parameter for  
which kind of assets a user wants to search (videos, pictures, …).  
Now I had the following idea, which is not working for me and clearly  
has some misunderstanding of OO. 🙂

I created a class called Media which looks like this (pseudo-code):

class Media  
def initialize(asset\_type)  
case asset\_type  
when “picture”  
return Picture  
when “video”  
return Video  
end  
end  
end

In my gallery-controller, I have a before-filter similar to:  
params[:media] = picture  
@media = Media.new(params[:media])

Later, I expect to do something like:  
@media.find(:all)

The important thing is, that I only want to see pictures!

I see that it is not possible to return a complete class definition.  
Even returning a “Picture.new” doesn’t give me my methods defined in  
the Picture model. I also don’t expect to see @media.find(:all)  
working, if @media was created with a Picture.new.

Does anybody has hints in which direction to look or how to solve this  
problem?

Thanks in advance!

Cheers  
Markus

---

<div class="post-metadata">

**Author:** ![lordbyte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/77aa72/32.png) [@lordbyte](https://www.ruby-forum.com/u/lordbyte)\
**Post date:** [May 27, 2007, 5:15pm UTC](https://www.ruby-forum.com/t/return-a-complete-class/105225/2 "2007-05-27T17:15:28Z")

</div>

Hi!

[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/3deb36e25b583b8d/4e39ddef64babb1c?lnk=gst&q=factory+class&rnum=44#4e39ddef64babb1c](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/3deb36e25b583b8d/4e39ddef64babb1c?lnk=gst&q=factory+class&rnum=44#4e39ddef64babb1c)

Looks like this will solve my problem. 🙂
