# Define\_method and alias\_method is not relevant to class\_eval

**URL:** <https://www.ruby-forum.com/t/define-method-and-alias-method-is-not-relevant-to-class-eval/84612>\
**Category:** Ruby\
**Created:** [February 6, 2007, 12:21pm UTC](https://www.ruby-forum.com/t/define-method-and-alias-method-is-not-relevant-to-class-eval/84612 "2007-02-06T12:21:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [February 6, 2007, 12:21pm UTC](https://www.ruby-forum.com/t/define-method-and-alias-method-is-not-relevant-to-class-eval/84612/1 "2007-02-06T12:21:04Z")

</div>

Hi.

I have a question on the semantics of define\_method and alias\_method.  
Let me start with class\_eval and instance\_eval. Following is the  
excerpt from ri class\_eval and ri instance\_eval:

- Module.class\_eval  
Evaluates the string or block in the context of mod. This can be used  
to add methods to a class. module\_eval returns the result of  
evaluating its argument.

- Object.instance\_eval  
Evaluates a string containing Ruby source code, or the given block,  
within the context of the receiver (obj). In order to set the context,  
the variable self is set to obj while the code is executing, giving  
the code access to obj’s instance variables. In the version of  
instance\_eval that takes a String, the optional second and third  
parameters supply a filename and starting line number that are used  
when reporting compilation errors.

Therefore, given class Foo; end, Foo.class\_eval interprets given block  
while treating Foo as a class. Hence, because Foo is a class,  
following code adds instance method “instance\_hello2”.

Foo.class\_eval %{  
# Add f.instance\_hello2 and  
# g.instance\_hello2  
#  
# class Foo  
# def instance\_hello2  
# “instance\_hello2”  
# end  
# end  
#  
def instance\_hello2  
“instance\_hello2”  
end  
}

On the other hand, Foo.instance\_eval interprets given block while  
treating Foo itself as the context . Hence, following code adds class  
method to “Foo.class\_hello2”:

Foo.instance\_eval %{  
# Add Foo.class\_hello2  
#  
# def Foo.class\_hello2  
# “class\_hello2”  
# end  
def class\_hello2  
“class\_hello2”  
end  
}

But the problem is define\_method do not run differently whatever the  
context is:

Foo.class\_eval %q[  
define\_method(:yyy) { “yyy” }  
]

generates instance method yyy. And,

Foo.class\_eval %q[  
define\_method(:yyy) { “yyy” }  
]

also generates instance method yyy. Following code generates Foo.zzz  
regardless of the context, instance\_eval/class\_eval, too:

class \<\< Foo; self; end.instance\_eval %q[  
define\_method(:zzz) { “zzz” }  
]

And I wonder why instance\_eval/class\_eval do not matter when it comes  
to define\_method and alias\_method. “ri define\_method” gives me  
“Defines an instance method in the receiver. The method parameter can  
be a Proc or Method object. If a block is specified, it is used as the  
method body.”

So I guess instance\_eval and class\_eval set context, but the receiver  
is always the one precedes the instance\_eval and class\_eval clause. As  
an example,

Foo.instance\_eval { # Foo is the receiver  
# Interprets under the context of Foo object. Hence, add method to  
Foo itself, resulting in class method.  
def …  
end

```
# Because Foo is the receiver, an instance method is added to Foo.
define_method ...

```

}

On the other hand,

class \<\< Foo; self; end.class\_eval { # Foo’s metaclass(I’ll denote  
this as (Foo) fron now on) is the receiver  
# Interprets treating (Foo) as a class. Hence, Foo’s class method  
like Foo.bar is generated.  
def …  
end

```
# Because (Foo) is the receiver, an instance method is added to

```

(Foo). And this means  
# methods are added to Foo which is the instance of (Foo).  
define\_method …  
}

Did I miss anything? Please verify whether my reasoning is correct.

Sincerely Yours,  
Minkoo S.

---

<div class="post-metadata">

**Author:** ![pool007](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pool007](https://www.ruby-forum.com/u/pool007)\
**Post date:** [February 8, 2007, 3:35pm UTC](https://www.ruby-forum.com/t/define-method-and-alias-method-is-not-relevant-to-class-eval/84612/2 "2007-02-08T15:35:24Z")

</div>

Minkoo S. schrieb:

> I have a question on the semantics of define\_method and alias\_method.  
> Let me start with class\_eval and instance\_eval. (…)

Hi. Have you seen [ruby-talk:207801]? Does it answer you questions? If  
not, feel free to ask again.

Regards,  
Pit
