# Working with classname that is stored in a string

**URL:** <https://www.ruby-forum.com/t/working-with-classname-that-is-stored-in-a-string/111966>\
**Category:** Ruby\
**Created:** [August 13, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/working-with-classname-that-is-stored-in-a-string/111966 "2007-08-13T22:45:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 13, 2007, 10:45pm UTC](https://www.ruby-forum.com/t/working-with-classname-that-is-stored-in-a-string/111966/1 "2007-08-13T22:45:07Z")

</div>

Hi all,

I’m trying to DRY my latest Rails project and have run into a problem  
for  
which the answer appears to lie in my personal sea of ignorance.

I want to write a helper that will let me set the fields I want to  
display  
before I start looping.

Basically I wish to pass the class name and an array of fields to be  
ignored  
to the helper and have it give me an array of the fields I want.

The problem is this, I have no idea how to get Ruby to treat the string  
I  
pass as a classname. I did some searching and didn’t find anything that  
looked like it would solve my problem.

I’d rather not have to create a new instance if I don’t have to.

Thanks,  
Glen

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 13, 2007, 10:58pm UTC](https://www.ruby-forum.com/t/working-with-classname-that-is-stored-in-a-string/111966/2 "2007-08-13T22:58:09Z")

</div>

Glen H. wrote:

> The problem is this, I have no idea how to get Ruby to treat the string I  
> pass as a classname. I did some searching and didn’t find anything that  
> looked like it would solve my problem.

I believe you are looking for const\_get.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 13, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/working-with-classname-that-is-stored-in-a-string/111966/3 "2007-08-13T23:26:39Z")

</div>

Glen H. wrote:

> Hi all,

One thing you can do is not use a string at all, but pass the class name  
constant. If you’ve got a class like:

class MyClass  
…  
end

then you can do something like:

filtered\_fields = filter\_my\_fields MyClass, [:field1, :field2]

and it works just fine.

Alternatively, you could use an AR extension like this:

class ActiveRecord::Base  
def filtered\_fields(\*fields)  
@attributes.reject{|name, value| fields.include?(name.intern)}  
end  
end

Haven’t run that code, but that should let you do something like  
my\_ar\_instance.filtered\_fields(:field1, :field2)

---

<div class="post-metadata">

**Author:** ![Glen\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/34f0e0/32.png) [@Glen\_H](https://www.ruby-forum.com/u/Glen_H)\
**Post date:** [August 13, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/working-with-classname-that-is-stored-in-a-string/111966/4 "2007-08-13T23:29:18Z")

</div>

Thanks!

On 8/13/07, Sebastian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## HTH, Sebastian
> 
> Jabber: removed\_email\_address@domain.invalid  
> ICQ: 205544826

–  
“Hey brother Christian with your high and mighty errand, Your actions  
speak  
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)
