# Is eval the only way?

**URL:** <https://www.ruby-forum.com/t/is-eval-the-only-way/56941>\
**Category:** Rails\
**Created:** [April 5, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941 "2006-04-05T23:40:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [April 5, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941/1 "2006-04-05T23:40:32Z")

</div>

Hi,

I’m trying to write some methods which use a parameter to represent a  
Class name.

A very simplistic example:

def foo(class\_name, conditions)  
x = class\_name.find(:all, :conditions =\> “#{conditions}”)  
end

Unfortunately, this does not work. The only way around this problem  
that I can think of is to use eval:

def foo(class\_name, conditions)  
x = eval “#{class\_name}.find(:all, :conditions =\> “#  
{conditions}’”)”  
end

There’s got to be a better way. – Please help!

: )

Jason

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [April 6, 2006, 12:23am UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941/2 "2006-04-06T00:23:20Z")

</div>

On Wed, Apr 05, 2006 at 05:39:35PM -0400, Jason T. wrote:  
} I’m trying to write some methods which use a parameter to represent a  
} Class name.  
}  
} A very simplistic example:  
}  
} def foo(class\_name, conditions)  
} x = class\_name.find(:all, :conditions =\> “#{conditions}”)  
} end  
}  
} Unfortunately, this does not work. The only way around this problem  
} that I can think of is to use eval:  
[…]  
} There’s got to be a better way. – Please help!

def foo(class\_name, conditions)  
x = Kernel.const\_get(class\_name).find(:all, :conditions =\>  
“#{conditions}”)  
end

} : )  
} Jason  
–Greg

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [April 6, 2006, 12:26am UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941/3 "2006-04-06T00:26:22Z")

</div>

On Apr 05, 2006, at 10:39 pm, Jason T. wrote:

> : )
> 
> Jason

Jason

Try this:

def foo(class\_name, conditions)  
ObjectSpace.each\_object(Class) do |c|  
return c.find(:all, :conditions =\> conditions) if c.name ==  
class\_name  
end  
end

You’d have to benchmark it to see which performs better, but it looks  
more “Ruby-like” like that.

Ashley

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [April 6, 2006, 12:26am UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941/4 "2006-04-06T00:26:23Z")

</div>

On Apr 05, 2006, at 11:23 pm, Gregory S. wrote:

> def foo(class\_name, conditions)  
> x = Kernel.const\_get(class\_name).find(:all, :conditions =\> “#  
> {conditions}”)  
> end

Meh… that beats mine 😛

Ashley

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [April 6, 2006, 2:08am UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941/5 "2006-04-06T02:08:21Z")

</div>

> > def foo(class\_name, conditions)  
> > x = Kernel.const\_get(class\_name).find(:all, :conditions =\> "#

ive been using Object’s const\_get instead of Kernel’s. is there a  
difference? and why is there an object and a kernel if not?

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [April 6, 2006, 2:30am UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941/6 "2006-04-06T02:30:30Z")

</div>

On Thu, Apr 06, 2006 at 02:08:21AM +0200, carmen wrote:  
}  
} \>\> def foo(class\_name, conditions)  
} \>\> x = Kernel.const\_get(class\_name).find(:all, :conditions =\> "#  
}  
} ive been using Object’s const\_get instead of Kernel’s. is there a  
} difference? and why is there an object and a kernel if not?

No, there isn’t:

irb(main):001:0\> Object.included\_modules  
=\> [Kernel]  
irb(main):002:0\>

–Greg

---

<div class="post-metadata">

**Author:** ![jtoy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jtoy](https://www.ruby-forum.com/u/jtoy)\
**Post date:** [April 6, 2006, 12:56am UTC](https://www.ruby-forum.com/t/is-eval-the-only-way/56941/7 "2006-04-06T00:56:46Z")

</div>

On 4/5/06, Jason T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Unfortunately, this does not work. The only way around this problem  
> that I can think of is to use eval:
> 
> def foo(class\_name, conditions)  
> x = eval “#{class\_name}.find(:all, :conditions =\> "#  
> {conditions}'")”  
> end
> 
> There’s got to be a better way. – Please help!

Inside Rails, you can do:  
x = class\_name.constantize.find(:all, :conditions =\> “#{conditions}”)

constantize is a more-readable shortcut for the const\_get business.
