# Difference between class\_eval and just adding the function?

**URL:** <https://rubytalk.org/t/difference-between-class-eval-and-just-adding-the-function/44353>\
**Category:** ruby-talk\
**Created:** [7 February 2008 23:55 UTC](https://rubytalk.org/t/difference-between-class-eval-and-just-adding-the-function/44353 "2008-02-07T23:55:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aryk\_Grosz](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Aryk\_Grosz](https://rubytalk.org/u/Aryk_Grosz)\
**Post date:** [7 February 2008 23:55 UTC](https://rubytalk.org/t/difference-between-class-eval-and-just-adding-the-function/44353/1 "2008-02-07T23:55:05Z")

</div>

Lets say I have

module A  
class B  
def t  
puts "hi"  
end  
end  
end

And I want to add a function to it. Whats the difference between:

module A  
class B  
def new\_function  
puts "foo"  
end  
end  
end

\*\*\*AND\*\*\*

def new\_function  
puts "foo"  
end  
end

Is there any difference?

> **···**
>
> A::B.class\_eval do  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Day](https://avatars.discourse-cdn.com/v4/letter/d/dc4da7/32.png) [@Day](https://rubytalk.org/u/Day)\
**Post date:** [8 February 2008 00:23 UTC](https://rubytalk.org/t/difference-between-class-eval-and-just-adding-the-function/44353/2 "2008-02-08T00:23:52Z")

</div>

Kind of hard to read all this nesting without some kind of spacing for  
help. Or did it just get stripped for me, for some reason?

Anyway, I think, if you're writing out the method ahead of time,  
there's not a real difference. However, if you wanted to create  
methods on the fly based on something else (who knows what...), you'd  
want, of the two, class\_eval. Though, really, you'd probably want to  
use add\_method, or whatever the name of the function for doing that is  
(can't remember right now).

Ben

> **···**
>
> On Feb 7, 2008 5:55 PM, Aryk Grosz \<tennisbum2002@hotmail.com\> wrote:
> 
> > And I want to add a function to it. Whats the difference between:
> > 
> > module A  
> > class B  
> > def new\_function  
> > puts "foo"  
> > end  
> > end  
> > end
> > 
> > \*\*\*AND\*\*\*
> > 
> > A::B.class\_eval do  
> > def new\_function  
> > puts "foo"  
> > end  
> > end
> > 
> > Is there any difference?
