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

  • SEARCH
  • Home
  • 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 989311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:46:20+00:00 2026-05-16T05:46:20+00:00

In the following code include module is used. The way I see it if

  • 0

In the following code include module is used. The way I see it if include module is removed then also an instance method would be created. Then why user include module ?

http://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb#L1416

  include Module.new {
          class_eval <<-RUBY, __FILE__, __LINE__ + 1
            def destroy                     # def destroy
              super                         #   super
              #{reflection.name}.clear      #   posts.clear
            end                             # end
          RUBY
        }
  • 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-05-16T05:46:21+00:00Added an answer on May 16, 2026 at 5:46 am

    First of all let’s make one thing clear. When they call super inside the class_eval — it has absolutely nothing to do with why they used include Module.new {} thing. In fact the super which was called inside the destroy method is completely irrelevant to answering your question. There could be any arbitrary code inside that destroy method.

    Now that we got it out of the way, here’s what’s going on.

    In ruby, if you simply define a class method, and then define it again in the same class, you will not be able to call super to access the previous method.

    For example:

    class Foo
      def foo
        'foo'
      end
    
      def foo
        super + 'bar'
      end
    end
    
    Foo.new.foo # => NoMethodError: super: no superclass method `foo' for #<Foo:0x101358098>
    

    This makes sense, because the first foo was not defined in some superclass, or anywhere up the lookup chain (which is where super points). However, you can define the first foo in such a way that when you later overwrite it — it will be available by calling super. This is exactly what they wanted to achieve with doing module include.

    class Foo
      include Module.new { class_eval "def foo; 'foo' end" }
    
      def foo
        super + 'bar'
      end
    end
    
    Foo.new.foo # => "foobar"
    

    This works, because when you include a module, ruby inserts it into the lookup chain. This way you can subsequently call super in the second method, and expect the included method to be called. Great.

    However, you may wonder, why not simply include a module without all the tricks? Why are they using block syntax? We know that my above example is exactly equivalent to the following:

    module A
      def foo
        'foo'
      end
    end
    
    class Foo
      include A
    
      def foo
        super + 'bar'
      end
    end
    
    Foo.new.foo # => "foobar"
    

    So why didn’t they do that? The answer is — the call to reflection. They needed to capture the variable (or method) which was available in the current context, which is reflection.

    Since they are defining the new module using block syntax, all the variables outside of the block are available for usage inside the block. Convenient.

    Just to illustrate.

    class Foo
      def self.add_foo_to_lookup_chain_which_returns(something)
        # notice how I can use variable something in the class_eval string
        include Module.new { class_eval "def foo; '#{something}' end" }
      end
    end
    
    # so somewhere else I can do
    
    class Foo
      add_foo_to_lookup_chain_which_returns("hello")
    
      def foo
        super + " world"
      end
    end
    
    Foo.new.foo # => "hello world"
    

    Neat, huh?

    Now let me stress it again. The call to super inside of the destroy method in your example has nothing to do with any of the above. They called it for their own reasons, because maybe the class where this is happening is subclassing another class which already defined destroy.

    I hope this made it clear.

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

Sidebar

Related Questions

I have the following code: #include <iostream> #define BOOST_TEST_MODULE my test #include <boost/test/unit_test.hpp> BOOST_AUTO_TEST_CASE(
The following code #include <iostream> using namespace std; int main() { const char* const
The following code #include <stdio.h> template <typename T, T v> class Tem { T
I have the following code: #include<iostream> using namespace std; typedef void (*HandlerFunc)(int, int); HandlerFunc
I have the following code: #include <stdio.h> #include <pthread.h> #define THREAD_CNT 10 #define ITER
i have the following code: #include <stdio.h> int main(void) { float a[4] __attribute__((aligned(0x1000))) =
I have the following code: #include <string.h> int main(void) { char *buffer = NULL,
I have the following code: #include <iostream> #include boost/shared_ptr.hpp using boost::shared_ptr; class Base {
In the following code: #include <iostream> template <typename T, size_t N> void cal_size(T (&a)[N])
Consider the following code: #include <iostream> using namespace std; int main() { int x,

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.