Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7846043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:26:51+00:00 2026-06-02T17:26:51+00:00

Is there any difference in how class_eval & instance_eval work except def ? Inside

  • 0

Is there any difference in how class_eval & instance_eval work except def? Inside class_eval block def defines method to class itself (i.e. instance method) and inside instance_eval def defines method to the eigenclass of the class (i.e. class method). AFAIK all other features work identically in both cases (e.g. define_method, attr_accessor, class << self; end, defining constants). Is it true?

Answer is: def, undef and alias have different contexts for class_eval and instance_eval.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T17:26:53+00:00Added an answer on June 2, 2026 at 5:26 pm

    Long story short:

    • Object.instance_eval &block sets:
      • self to Object
      • The “current class” to Object.singleton_class
    • Object.class_eval &block sets:
      • self to Object
      • The “current class” to Object

    The “current class” is used for def, undef and alias, as well as constant and class variable lookups.


    Now, let’s have a look at the implementation details.

    Here’s how module_eval and instance_eval are implemented in C:

    VALUE rb_mod_module_eval(int argc, VALUE *argv, VALUE mod) {
        return specific_eval(argc, argv, mod, mod);
    }
    
    VALUE rb_obj_instance_eval(int argc, VALUE *argv, VALUE self) {
        VALUE klass;
        if (SPECIAL_CONST_P(self)) { klass = Qnil; }
        else { klass = rb_singleton_class(self); }
        return specific_eval(argc, argv, klass, self);
    }
    

    Both call specific_eval, which takes the following arguments: int argc, VALUE *argv, VALUE klass and VALUE self.

    Note that:

    • module_eval passes the Module or Class instance as both klass and self
    • instance_eval passes the object’s singleton class as klass

    If given a block, specific_eval will call yield_under, which takes the following arguments: VALUE under, VALUE self and VALUE values.

    if (rb_block_given_p()) {
        rb_check_arity(argc, 0, 0);
        return yield_under(klass, self, Qundef);
    }
    

    There are two important lines in yield_under:

    1. block.self = self;

      This sets the self of the block to the receiver.

    2. cref = vm_cref_push(th, under, NOEX_PUBLIC, blockptr);

      The cref is a linked list
      which specifies the “current class”, which is used for def, undef and alias, as well
      as constant and class variable lookups.

      That line basically sets the cref to under.

      Finally:

      • When called from module_eval, under will be the Class or Module
        instance.

      • When called from instance_eval, under will be the singleton class of
        self.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any difference if you define Foo with instance_eval: . . . class
Is there any difference between these tow pieces of code & which approach is
Is there any difference between type casting & type conversion in c++.
i wonder if there is any difference between class TestClass { private $_var =
Is there any difference between public class Controller1 extends AbstractController { @Override protected ModelAndView
Is there any difference on Scala 2.9.1: Array[String]().getClass and Java 7: String[].class ? If
Is there any difference between <opensearch:totalResults>1000</opensearch:totalResults> and <totalResults xmlns=opensearch>1000</totalResults> I'm using the SyndicationFeed class
Is there any difference to the following code: class Foo { inline int SomeFunc()
When extending a class, is there any difference in performance between polymorphism and composition?
Is there any difference between a Singleton class and a class with all static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.