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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:34:30+00:00 2026-05-12T10:34:30+00:00

I am trying to find a way to get the binding from the caller

  • 0

I am trying to find a way to get the binding from the caller within method_missing in Ruby (1.8), but I can’t seem to find a way to do it.

Hopefully the following code explains what I would like to do:

class A
  def some_method
    x = 123
    nonexistent_method
  end

  def method_missing(method, *args, &block)
    b = caller_binding # <---- Is this possible?
    eval "puts x", b
  end
end

A.new.some_method
# expected output:
#   123

So… is there a way to obtain the caller’s binding, or is this just impossible in Ruby (1.8)?

  • 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-12T10:34:30+00:00Added an answer on May 12, 2026 at 10:34 am

    Here’s a (somewhat fragile) hack:

    # caller_binding.rb
    TRACE_STACK = []
    VERSION_OFFSET = { "1.8.6" => -3, "1.9.1" => -2 }[RUBY_VERSION]
    def caller_binding(skip=1)
      TRACE_STACK[ VERSION_OFFSET - skip ][:binding]
    end
    set_trace_func(lambda do |event, file, line, id, binding, classname|
      item = {:event=>event,:file=>file,:line=>line,:id=>id,:binding=>binding,:classname=>classname}
      #p item
      case(event)
      when 'line'
        TRACE_STACK.push(item) if TRACE_STACK.empty?
      when /\b(?:(?:c-)?call|class)\b/
        TRACE_STACK.push(item)
      when /\b(?:(?:c-)?return|end|raise)\b/
        TRACE_STACK.pop
      end
    end)
    

    This works with your example, but I haven’t tested it with much else

    require 'caller_binding'
    class A
      def some_method
        x = 123
        nonexistent_method
      end
      def method_missing( method, *args, &block )
        b = caller_binding
        eval "puts x", b
      end
    end
    
    x = 456
    A.new.some_method #=> prints 123
    A.new.nonexistent_method #=> prints 456
    

    Of course, this won’t work if the binding doesn’t define the variable you’re trying to evaluate, but this is a general issue with bindings. If a variable is not defined, it doesn’t know what it is.

    require 'caller_binding'
    def show_x(b)
      begin
        eval <<-SCRIPT, b
          puts "x = \#{x}"
        SCRIPT
      rescue => e
        puts e
      end
    end
    
    def y
      show_x(caller_binding)
    end
    
    def ex1
      y #=> prints "undefined local variable or method `x' for main:Object"
      show_x(binding) #=> prints "undefined local variable or method `x' for main:Object"
    end
    
    def ex2
      x = 123
      y #+> prints "x = 123"
      show_x(binding) #+> prints "x = 123"
    end
    
    ex1
    ex2
    

    To get around this, you need to do some error handling within the evaluated string:

    require 'caller_binding'
    def show_x(b)
      begin
        eval <<-SCRIPT, b
          if defined? x
            puts "x = \#{x}"
          else
            puts "x not defined"
          end
        SCRIPT
      rescue => e
        puts e
      end
    end
    
    def y
      show_x(caller_binding)
    end
    
    def ex1
      y #=> prints "x not defined"
      show_x(binding) #=> prints "x not defined"
    end
    
    def ex2
      x = 123
      y #+> prints "x = 123"
      show_x(binding) #+> prints "x = 123"
    end
    
    ex1
    ex2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What I am trying to do seems simple, but I can't find a way.
I am trying to find a way to get a random selection from a
Im new to java trying to find a way to get info from facebook
I'm trying to find a way to get the total number of child nodes
So I'm trying to find a way to get a collection of all versions
I'm trying to find a good way to get a Scanner to use a
I am trying to find simple, elegant way to get all the pertinent name,value
Trying to find a way to remove blank pages from a document I wrote
Trying to find a way to send a POST HTTPS request from Python to
I'm trying to find a way to get rid of the border around the

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.