# Class\_eval and braces

**URL:** <https://www.ruby-forum.com/t/class-eval-and-braces/98057>\
**Category:** Ruby\
**Created:** [January 27, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/class-eval-and-braces/98057 "2006-01-27T21:17:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [January 27, 2006, 9:17pm UTC](https://www.ruby-forum.com/t/class-eval-and-braces/98057/1 "2006-01-27T21:17:22Z")

</div>

Hi,

as I notice there is a difference between `class\_eval’ called  
with a block or with a string. Why?

Bertram

* * *

VERSION = :braces

module M  
def self.included c  
case VERSION  
when :braces then c.class\_eval {@@m = rand}  
when :quotes then c.class\_eval “@@m = rand”  
end  
end  
end

class C ; @@m = “M” ; def d ; puts @@m ; end ; end  
class D ; @@m = “M” ; def d ; puts @@m ; end ; end

class C ; puts @@m ; end  
class D ; puts @@m ; end  
C.new.d  
D.new.d

class C ; include M ; end  
class D ; include M ; end

class C ; puts @@m ; end  
class D ; puts @@m ; end  
C.new.d  
D.new.d

---

<div class="post-metadata">

**Author:** ![Bertram\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/54ee81/32.png) [@Bertram\_S](https://www.ruby-forum.com/u/Bertram_S)\
**Post date:** [January 27, 2006, 10:35pm UTC](https://www.ruby-forum.com/t/class-eval-and-braces/98057/2 "2006-01-27T22:35:52Z")

</div>

On 2006.01.28 05:15, Bertram S. wrote:

> Hi,
> 
> as I notice there is a difference between `class\_eval’ called  
> with a block or with a string. Why?

Closures again. The @@m in the block is the @@m of M,  
not the including class. To see this, add a line to  
your code:

> class C ; puts @@m ; end  
> class D ; puts @@m ; end  
> C.new.d  
> D.new.d

module M; puts @@m; end

> Bertram

E
