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 7832557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:17:11+00:00 2026-06-02T12:17:11+00:00

define_method exhibits the following behaviour: class TestClass def exec_block(&block) ; yield ; end end

  • 0

define_method exhibits the following behaviour:

class TestClass
  def exec_block(&block) ; yield ; end
end
TestClass.new.send(:exec_block) do ; puts self ; end
# -> main
TestClass.send(:define_method, :bing) do ; puts self ; end
TestClass.new.bing
# -> <TestClass:...>

What I do not understand is that the the block passed to define_method is supposed to be a closure. As such it should (at least according to my understanding) capture the value of self as main, as exhibited when calling exec_block.

I understand that the block will become the body of the method, yet I do not understand the reason for the behavior. Why does the block evaluate to different things when used with different methods ?

How can I reproduce the behavior of block with define_method for other methods ? i.e. how could I write exec_block to have it output <TestClass:...> instead of `main´ ?

  • 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-02T12:17:15+00:00Added an answer on June 2, 2026 at 12:17 pm

    self is captured by the closure like any other variable. We can verify that by passing a Proc around different object instances:

    class A
      def exec_block(&block)
        block.call
      end
    end
    
    class B
      def exec_indirect(&block)
        A.new.exec_block(&block)
      end
    end
    
    block = proc { p self }
    a = A.new; b = B.new
    
    a.exec_block(&block)    # => main
    b.exec_indirect(&block) # => main
    

    However, BasicObject#instance_eval and the alikes rebind the self variable dynamically:

    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

    Module#define_method in turn uses instance_eval to execute the associated block:

    If a block is specified, it is used as the method body. This block is evaluated using instance_eval […]

    Observe:

    A.send(:define_method, :foo, &block)
    a.foo                   # => #<A:0x00000001717040>
    a.instance_eval(&block) # => #<A:0x00000001717040>
    

    With that knowledge, you can now rewrite your exec_block to use instance_eval:

    class A
      def exec_block(&block)
        instance_eval(&block)
      end
    end
    
    block = proc { p self }
    A.new.exec_block(&block)  # => #<A:0x00000001bb9828>
    

    As mentioned before, using instance_eval seems to be the only way to run a Proc instance with a modified context. It can be used to implement dynamic binding in Ruby.

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

Sidebar

Related Questions

Let there be class Example defined as: class Example def initialize(test='hey') self.class.send(:define_method, :say_hello, lambda
I have the next code: class Class def attr_checked(attribute, &validation) define_method #{attribute}= do |value|
look the demo code: class A def method_a puts this is method_a end end
I have the following: module Thing def self.included(base) base.send :extend, ClassMethods end module ClassMethods
guys. I create a class: class A def initialize &b instance_eval &b end def
def singleton_class class << self self end end class Human proc = lambda {
This does not seem to work: class Test private define_method :private_method do uh! end
Snippet 1: module A def cm(m,ret) class_eval do define_method(m.to_sym) do return ret end end
Suppose that I have the following python base class: class BaseClass(object): def a(): This
I have two classes: class Activity < ActiveRecord::Base belongs_to :activity_type def belongs_to_cat_a? self.activity_category ==

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.