# Ruby For Rails class\_eval question

**URL:** <https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708>\
**Category:** Rails\
**Created:** [October 10, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708 "2006-10-10T23:03:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [October 10, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708/1 "2006-10-10T23:03:11Z")

</div>

When I run the following example on page 349, I get the error message:

NoMethodError: undefined method `some\_method’ for #\<C:0x2e3d698 @x=1\>  
from (irb):94  
from ?:0

c = Class.new  
c.class\_eval do  
def some\_method  
puts “Created in class\_eval”  
end  
end  
c = C.new  
c.some\_method

How to run this example? I followed the suggestion on the Manning’s  
errata page with no luck. TIA.  
A.

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [October 11, 2006, 1:55am UTC](https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708/2 "2006-10-11T01:55:23Z")

</div>

It works now. Why does it have to be constant?

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [October 11, 2006, 1:03am UTC](https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708/3 "2006-10-11T01:03:43Z")

</div>

Bala P. \<bparanj@…\> writes:

> c = Class.new  
> Your non-capitalization above is incorrect. The new class should be a  
> constant.  
> Try C = Class.new  
> c.class\_eval do

> def some\_method  
> puts “Created in class\_eval”  
> end  
> end  
> c = C.new  
> Well, you haven’t defined C because of the capitalization issue above.  
> c.some\_method

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [October 11, 2006, 2:03am UTC](https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708/4 "2006-10-11T02:03:46Z")

</div>

Bala P. wrote:

> It works now. Why does it have to be constant?

It doesn’t really have to be. It’s just a convention that classes are  
defined into constants.

You can just as easily do:

x = Class.new  
x.class\_eval do  
def some\_method  
puts “Created in class\_eval”  
end  
end  
c = x.new  
c.some\_method

The reason your first example failed was b/c you were inconsistently  
mixing ‘c’ and ‘C’.

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [October 11, 2006, 3:15am UTC](https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708/5 "2006-10-11T03:15:28Z")

</div>

Hi –

On Tue, 10 Oct 2006, Bala P. wrote:

> puts “Created in class\_eval”  
> end  
> end  
> c = C.new  
> c.some\_method
> 
> How to run this example? I followed the suggestion on the Manning’s errata page with no luck. TIA.

The erratum has an erratum… The two lines I provided are run  
together into one. They should be split, as below.

The whole thing should be:

c = Class.new  
c.class\_eval do  
def some\_method  
puts “Created in class\_eval”  
end  
end

c\_instance = c.new  
c\_instance.some\_method

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![bparanj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e9a140/32.png) [@bparanj](https://www.ruby-forum.com/u/bparanj)\
**Post date:** [October 11, 2006, 3:17am UTC](https://www.ruby-forum.com/t/ruby-for-rails-class-eval-question/71708/6 "2006-10-11T03:17:28Z")

</div>

Hi –

On Wed, 11 Oct 2006, Kian Wright wrote:

> x.class\_eval do  
> def some\_method  
> puts “Created in class\_eval”  
> end  
> end  
> c = x.new  
> c.some\_method
> 
> The reason your first example failed was b/c you were inconsistently  
> mixing ‘c’ and ‘C’.

My fault – it was wrong in the first printing, and the erratum has  
two lines run together as one. (See my other reply on this thread for  
the working version.)

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)
