# Class\_eval vs (class \<\< object)

**URL:** <https://www.ruby-forum.com/t/class-eval-vs-class-object/132957>\
**Category:** Ruby\
**Created:** [March 26, 2008, 5:00am UTC](https://www.ruby-forum.com/t/class-eval-vs-class-object/132957 "2008-03-26T05:00:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [March 26, 2008, 5:00am UTC](https://www.ruby-forum.com/t/class-eval-vs-class-object/132957/1 "2008-03-26T05:00:42Z")

</div>

What is the difference between class\_eval and “class \<\< object”?

It seems like they are the same. I am thinking that class\_eval affects  
the greater class and “class\_object” is used on a singleton class. Are  
they mutually exclusive?

Thanks

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [March 26, 2008, 7:23am UTC](https://www.ruby-forum.com/t/class-eval-vs-class-object/132957/2 "2008-03-26T07:23:00Z")

</div>

Ruby F. wrote:

> What is the difference between class\_eval and “class \<\< object”?
> 
> It seems like they are the same. I am thinking that class\_eval affects  
> the greater class and “class\_object” is used on a singleton class. Are  
> they mutually exclusive?
> 
> Thanks

class Dog  
attr\_accessor :age

def initialize(a)  
@age = a  
end  
end

Dog.class\_eval{  
def show  
puts @age  
end  
}

d1 = Dog.new(5)  
d2 = Dog.new(2)

d1.show #5  
d2.show #2

class Dog  
attr\_accessor :age

def initialize(a)  
@age = a  
end  
end

d1 = Dog.new(5)

class \<\< d1  
def show  
puts age  
end  
end

d1.show #5

d2 = Dog.new(2)  
d2.show

–output:–  
undefined method `show’ for #\<Dog:0x25260 @age=2\> (NoMethodError)

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [March 26, 2008, 7:28am UTC](https://www.ruby-forum.com/t/class-eval-vs-class-object/132957/3 "2008-03-26T07:28:40Z")

</div>

Ruby F. wrote:

> What is the difference between class\_eval and “class \<\< object”?
> 
> It seems like they are the same. I am thinking that class\_eval affects  
> the greater class and “class\_object” is used on a singleton class.

> Are  
> they mutually exclusive?

class Dog  
attr\_accessor :age

def initialize(a)  
@age = a  
end  
end

class \<\< Dog  
def greet  
puts “hello”  
end  
end

Dog.class\_eval{  
def show  
puts @age  
end  
}

Dog.greet #hello  
d = Dog.new(5)  
d.show #5

---

<div class="post-metadata">

**Author:** ![rubyfreak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@rubyfreak](https://www.ruby-forum.com/u/rubyfreak)\
**Post date:** [March 26, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/class-eval-vs-class-object/132957/4 "2008-03-26T12:27:59Z")

</div>

Hi –

On Wed, 26 Mar 2008, Ruby F. wrote:

> What is the difference between class\_eval and “class \<\< object”?
> 
> It seems like they are the same. I am thinking that class\_eval affects  
> the greater class and “class\_object” is used on a singleton class. Are  
> they mutually exclusive?

class \<\< object is the class keyword, which starts a class definition  
block (in this case, on the singleton class of object) and therefore a  
new local scope.

class\_eval is a method, not a keyword, so it requires a receiver –  
namely, a Module or Class object. It takes a block, and the block does  
shares the variables of the existing local scope.

David
