define_method and alias_method is not relevant to class_eval and instance_eval

Discussion in 'Ruby' started by Minkoo Seo, Feb 6, 2007.

  1. Minkoo Seo

    Minkoo Seo Guest


    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

    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

    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 ...

    # 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 is generated.
    def ...

    # 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 Seo
    Minkoo Seo, Feb 6, 2007
    1. Advertisements

  2. Pit Capitain

    Pit Capitain Guest

    Re: define_method and alias_method is not relevant to class_evaland instance_eval

    Minkoo Seo 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.

    Pit Capitain, Feb 8, 2007
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew Gibson

    instance_eval vs. class_eval context

    Andrew Gibson, Aug 10, 2006, in forum: Ruby
    James Edward Gray II
    Aug 11, 2006
  2. Artur Merke

    instance_eval && class_eval

    Artur Merke, Oct 21, 2006, in forum: Ruby
  3. Bharat Ruparel
    Sebastian Hungerecker
    Mar 8, 2008
  4. Christopher J. Bottaro

    instance_eval or class_eval on metaclass?

    Christopher J. Bottaro, Jul 11, 2008, in forum: Ruby
    Christopher J. Bottaro
    Jul 15, 2008
  5. Adriano Nagel

    define_method inside instance_eval

    Adriano Nagel, May 20, 2010, in forum: Ruby
    Dhruva Sagar
    May 20, 2010

Share This Page