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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:34:57+00:00 2026-06-01T04:34:57+00:00

Take this example Proc: proc = Proc.new {|x,y,&block| block.call(x,y,self.instance_method)} It takes two arguments, x

  • 0

Take this example Proc:

proc = Proc.new {|x,y,&block| block.call(x,y,self.instance_method)}

It takes two arguments, x and y, and also a block.

I want to execute that block using different values for self. Something like this nearly works:

some_object.instance_exec("x arg", "y arg", &proc)

However that doesn’t allow you to pass in a block. This also doesn’t work

some_object.instance_exec("x arg", "y arg", another_proc, &proc)

nor does

some_object.instance_exec("x arg", "y arg", &another_proc, &proc)

I’m not sure what else could work here. Is this possible, and if so how do you do it?

Edit: Basically if you can get this rspec file to pass by changing the change_scope_of_proc method, you have solved my problem.

require 'rspec'

class SomeClass
  def instance_method(x)
    "Hello #{x}"
  end
end

class AnotherClass
  def instance_method(x)
    "Goodbye #{x}"
  end

  def make_proc
    Proc.new do |x, &block|
      instance_method(block.call(x))
    end
  end
end

def change_scope_of_proc(new_self, proc)
  # TODO fix me!!!
  proc
end

describe "change_scope_of_proc" do
  it "should change the instance method that is called" do
    some_class = SomeClass.new
    another_class = AnotherClass.new
    proc = another_class.make_proc
    fixed_proc = change_scope_of_proc(some_class, proc)
    result = fixed_proc.call("Wor") do |x|
      "#{x}ld"
    end

    result.should == "Hello World"
  end
end
  • 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-01T04:34:58+00:00Added an answer on June 1, 2026 at 4:34 am

    To solve this, you need to re-bind the Proc to the new class.

    Here’s your solution, leveraging some good code from Rails core_ext:

    require 'rspec'
    
    # Same as original post
    
    class SomeClass
      def instance_method(x)
        "Hello #{x}"
      end
    end
    
    # Same as original post
    
    class AnotherClass
      def instance_method(x)
        "Goodbye #{x}"
      end
    
      def make_proc
        Proc.new do |x, &block|
          instance_method(block.call(x))
        end
      end
    end
    
    ### SOLUTION ###
    
    # From activesupport lib/active_support/core_ext/kernel/singleton_class.rb
    
    module Kernel
      # Returns the object's singleton class.
      def singleton_class
        class << self
          self
        end
      end unless respond_to?(:singleton_class) # exists in 1.9.2
    
      # class_eval on an object acts like singleton_class.class_eval.
      def class_eval(*args, &block)
        singleton_class.class_eval(*args, &block)
      end
    end
    
    # From activesupport lib/active_support/core_ext/proc.rb 
    
    class Proc #:nodoc:
      def bind(object)
        block, time = self, Time.now
        object.class_eval do
          method_name = "__bind_#{time.to_i}_#{time.usec}"
          define_method(method_name, &block)
          method = instance_method(method_name)
          remove_method(method_name)
          method
        end.bind(object)
      end
    end
    
    # Here's the method you requested
    
    def change_scope_of_proc(new_self, proc)
      return proc.bind(new_self)
    end
    
    # Same as original post
    
    describe "change_scope_of_proc" do
      it "should change the instance method that is called" do
        some_class = SomeClass.new
        another_class = AnotherClass.new
        proc = another_class.make_proc
        fixed_proc = change_scope_of_proc(some_class, proc)
        result = fixed_proc.call("Wor") do |x|
          "#{x}ld"
        end
        result.should == "Hello World"
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

take this example from google docs class BrowseHandler(webapp.RequestHandler): > def get(self, category, product_id): >
Take this example: int[] queryValues1 = new int[10] {0,1,2,3,4,5,6,7,8,9}; int[] queryValues2 = new int[100];
Take this example code (ignore it being horribly inefficient for the moment) let listToString
Ok, I'm very simply trying to take this example... http://jsfiddle.net/shanabus/HGF59/ and put it on
I'm using SharedPreferences to persists Object fields. Take this example: class Item { private
I need some help adding conditions to a linq query. Take this small example
Take this class as example: public class Category : PersistentObject<int> { public virtual string
I just wanted to confirm the difference here, take this as an example: class
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Take this code snippet for example: window.location.href = 'mysite.htm#images'; Already being on the site

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.