# Dynamically Create Class. (or. Why is eval() messy?)

**URL:** <https://rubytalk.org/t/dynamically-create-class-or-why-is-eval-messy/48486>\
**Category:** ruby-talk\
**Created:** [20 August 2008 20:28 UTC](https://rubytalk.org/t/dynamically-create-class-or-why-is-eval-messy/48486 "2008-08-20T20:28:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Patrick\_Li](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@Patrick\_Li](https://rubytalk.org/u/Patrick_Li)\
**Post date:** [20 August 2008 20:28 UTC](https://rubytalk.org/t/dynamically-create-class-or-why-is-eval-messy/48486/1 "2008-08-20T20:28:09Z")

</div>

Hi,  
I'm trying to create a utility method that will automatically create a  
class for me, given a classname. But I can't find a way around having to  
use eval().

This is what I'm doing right now:

def createClass(className)  
&nbsp;&nbsp;eval(\<\<-EOS)  
&nbsp;&nbsp;&nbsp;&nbsp;#{className} = Class.new do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#bla bla bla  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;EOS  
end

but I really don't like eval() and would like to avoid it, if at all  
possible.

Is there a reason, that Ruby doesn't support something like this: Maybe  
because it's inconsistent with something, or because it's really hard?

className = "MyClass"  
methodName = "myMethod"

class \*className  
&nbsp;&nbsp;def \*methodName  
&nbsp;&nbsp;&nbsp;&nbsp;puts "yay!"  
&nbsp;&nbsp;end  
end

Also, I find eval() code really messy. Though I'm not sure why. I think  
I don't like it, because my editor (IntelliJ) doesn't do syntax  
highlighting for strings. Do you guys know of any editors that will  
syntax highlight a eval() string for you?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [20 August 2008 20:40 UTC](https://rubytalk.org/t/dynamically-create-class-or-why-is-eval-messy/48486/2 "2008-08-20T20:40:21Z")

</div>

You're almost there. This works

def create\_class name, mod = Object  
&nbsp;&nbsp;cls = Class.new do  
&nbsp;&nbsp;&nbsp;&nbsp;...  
&nbsp;&nbsp;end  
&nbsp;&nbsp;mod.constant\_set name, cls  
end

This allows to put the created class inside any module/class you like. If you  
don't need it, simply remove it from the argument list and use Object in its  
place inside the method.

I hope this helps

Stefano

> **···**
>
> On Wednesday 20 August 2008, Patrick Li wrote:
> 
> > Hi,  
> > I'm trying to create a utility method that will automatically create a  
> > class for me, given a classname. But I can't find a way around having to  
> > use eval().
> > 
> > This is what I'm doing right now:
> > 
> > def createClass(className)  
> > &nbsp;&nbsp;eval(\<\<-EOS)  
> > &nbsp;&nbsp;&nbsp;&nbsp;#{className} = Class.new do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#bla bla bla  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;EOS  
> > end
> > 
> > but I really don't like eval() and would like to avoid it, if at all  
> > possible.

---

<div class="post-metadata">

**Author:** ![Patrick\_Li](https://avatars.discourse-cdn.com/v4/letter/p/e36b37/32.png) [@Patrick\_Li](https://rubytalk.org/u/Patrick_Li)\
**Post date:** [20 August 2008 20:44 UTC](https://rubytalk.org/t/dynamically-create-class-or-why-is-eval-messy/48486/3 "2008-08-20T20:44:01Z")

</div>

Ah =), that's quite lovely. Thank you.  
Until you suggested that, I was almost contemplating switching to Lisp  
or something.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
