# Using variable class names, how?

**URL:** <https://www.ruby-forum.com/t/using-variable-class-names-how/161682>\
**Category:** Rails\
**Created:** [February 28, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682 "2009-02-28T22:28:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [February 28, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/1 "2009-02-28T22:28:37Z")

</div>

Hello,

I’ve got some code, which I would like to reuse. The only difference  
between the usages are the class names and therefore the different  
objects. How would you manage this? Is there a way to take the class  
names as strings? Do you know other solutions, a suitable pattern, for  
example?

Thanx for any suggestions!

Best regards, ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [February 28, 2009, 10:43pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/2 "2009-02-28T22:43:53Z")

</div>

Start by reading this →   
[http://groups.google.com/group/rubyonrails-talk/browse\_thread/thread/dce05002d11b4849?hl=en#](http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/dce05002d11b4849?hl=en#)

The group archive is always a good place to start your search before  
sending messages.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 1, 2009, 3:15pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/3 "2009-03-01T15:15:54Z")

</div>

Thanks for your answer, I read the thread, but this did not really  
help me. It’s not just about redefining some core methods. I think,  
the description of my problem was not clear enough, sorry for this. I  
would appreciate, if you could have a look at the following  
explanation.

I’ve got several entities saved in my database: E1, E2 … En. All  
these entities can be flexibly annotated, so I’ve got the following  
models:

## E1 (Entity) =\> The entity itself EA1 (EntityAnnotation) =\> The various entity annotations, may be nested

ET (EntityType) =\> The used entity type which tells something about  
the used datatype, display properties, etc.

This group of models repeat themselves for each exposed entity, so  
I’ve got:

## E1…n EA1…n

ET

Now comes the point, I was originally refering to:

I created some code which checks, if the given annotation type can be  
applied to the entity, what kind of sub-entities (children) the  
annotation might can have and how much of them and then save them to  
the entity annotation table. Here’s some pseudo-code for this:

1: fetch all annotations / attributes which should be attached to a  
entity (those data comes from html formular)  
2: check, whether the given annotations can be applied to the entity  
and if they have the correct value type  
3: if there are sub-annotations, check whether they are valid and do  
not exceed the maximum of allowed children and if they also have the  
correct value type  
4: (check more various constraints…)  
5: (maybe, some more stuff to do…)  
6: if there’s no error, save the annotations with the given types and  
values

As I want to save alle entites separately for some kind of reason, I  
come to this (refering to the pseudo code above):

1: fetch the data  
2: validate given annotations regarding E1  
3: validate given annotation children regarding E1  
4: …  
5: …  
6: no error: save E1 and all corresponding EA1

For the next entity I would repeat all the stuff, just changing the  
class names / object names, but gaining all the logic. The point is,  
that I don’t want to use some kind of case distinction directly in the  
code.

The class using this modul should just define a string constant  
defining which entity class / entity annotation class to use. I just  
need a possibility to handle class names as string an vice versa like  
I do when using the core “send(meth\_name, attr, &block)” method.

I hope, this explanation is adequate.

Thanx for your help!

Best regards, ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 1, 2009, 5:08pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/4 "2009-03-01T17:08:07Z")

</div>

Sorry. but i’m completely clueless. Can’t really understand what  
you’re trying to say/do.

- 

Maurício Linhares  
[http://alinhavado.wordpress.com/](http://alinhavado.wordpress.com/) (pt-br) | [http://blog.codevader.com/](http://blog.codevader.com/)  
(en)

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 1, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/5 "2009-03-01T17:26:54Z")

</div>

Hey, no problem, thank you for taking time! 🙂

– ms

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 1, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/6 "2009-03-01T17:53:27Z")

</div>

On Mar 1, 4:26 pm, ms [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey, no problem, thank you for taking time! 🙂

Is “Foo”.constantize what you’re looking for ?

Fred

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 1, 2009, 8:11pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/7 "2009-03-01T20:11:13Z")

</div>

Instead of passing class names as strings, why not ref the classes  
themselves? Lame example, but … something like:

…  
def first\_last\_count(clss)  
return [clss.find(:first), clss.find(:last), clss.count]  
end

…  
first\_car,last\_car,count\_cars = first\_last\_count(Car)  
…  
first\_bike,last\_bike,count\_bikes = first\_last\_count(Bike)  
…

Jeff

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 1, 2009, 8:43pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/8 "2009-03-01T20:43:28Z")

</div>

My example used clss not class, … but sure klass if you prefer.

Jeff

---

<div class="post-metadata">

**Author:** ![msmsms](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/34f0e0/32.png) [@msmsms](https://www.ruby-forum.com/u/msmsms)\
**Post date:** [March 1, 2009, 8:33pm UTC](https://www.ruby-forum.com/t/using-variable-class-names-how/161682/9 "2009-03-01T20:33:29Z")

</div>

Jeff L. wrote:

> def first\_last\_count(clss)

Note the tradition there is klass:

[http://google.com/codesearch?q=lang%3Aruby+klass](http://google.com/codesearch?q=lang%3Aruby+klass)

Being ‘class’ is a keyword…

–  
Phlip
