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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:16:22+00:00 2026-06-15T11:16:22+00:00

I have the following code: def call_block Proc.new.call my_local_proc = Proc.new { Proc.new.call }

  • 0

I have the following code:

def call_block
  Proc.new.call
  my_local_proc = Proc.new { Proc.new.call }
  my_local_proc.call
end

call_block { p 'block' }

The output is:

block
block

Can someone explain to me how Proc.new found the block I passed to call_block?
I guess that Proc.new just searches for the closest block and that it`s implemented entirely in C++.

And I have another question: Can something like this be achieve using only ruby? I mean,
can I write a method such that, if not block has been given, takes the block that was passed to the method calling it. Something like:

def bar
  if not block_given?
    #use the block that has been given to the caller
  end
  # some code
end

def foo
  bar
end

foo { :block }
  • 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-15T11:16:23+00:00Added an answer on June 15, 2026 at 11:16 am

    Proc.new will use the method’s block if called without one inside a method with one attached. This is documented behavior.

    To find out how YARV does it, let’s read the source code. Specifically, the proc_new function:

    block_pointer = rb_vm_control_frame_block_ptr(control_frame_pointer);
    

    This line retrieves a pointer to the block associated with the current control frame.

    I believe these control frames implement Ruby’s stack. We are currently inside the Proc.new control frame, so this would retrieve the pointer to the block given to the method.

    if (block_pointer != NULL) {
        /* block found */
    } else {
        /* block not found... */
    }
    

    If the pointer isn’t NULL, then Proc.new was passed a block explicitly. What if the pointer is NULL, though?

    /* block not found... */
    control_frame_pointer = RUBY_VM_PREVIOUS_CONTROL_FRAME(control_frame_pointer);
    block_pointer = rb_vm_control_frame_block_ptr(control_frame_pointer);
    

    We move up on the stack and try to get its block. In other words, we move up to the caller’s control frame and try to get its block.

    if (block_pointer != NULL) {
        if (is_lambda) {
            rb_warn("tried to create Proc object without a block");
        }
    } else {
        rb_raise(rb_eArgError, "tried to create Proc object without a block");
    }
    

    Now, if it’s not NULL, then we pretty much succeeded. If it’s still NULL, then we can’t create a Proc, so we raise an ArgumentError.

    The algorithm boils down to this:

    1. See if Proc.new was given a block
      1. If so, use it
      2. If not, see if caller was given a block
        1. If so, use it
        2. If not, raise error

    Source code altered for readability. Visit linked source file on GitHub for the original.

    • 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: def maturities InfoItem.find_all_by_work_order(self.work_order).map(&:maturity) end I was thinking about changing
I have the following code: #!/usr/bin/ruby class Person def self.speak p = self.new puts
I have the following code: def show @game = $site.new_game_of_type(params[:id]) @game.start end def update_game_time
I have following code: module CarHelper def call_helpline puts Calling helpline... end end class
I have the following code: def bootstrap_form_field(f, method, text, &block) content_tag :div, :class =>
I have following code: def whatever(url, data=None): req = urllib2.Request(url) res = urllib2.urlopen(req, data)
I have the following code: def query(self,query): lock = QMutexLocker(self.mutex) reply = self.conn.query(query) if
I have the following code: def add_record(self,values): self.sql.execute(INSERT INTO TEST VALUES (?,?) % values)
i have the following code def get(self): date = datetime.date.today() loc_query = Location.all() last_cursor
I have the following code: def input_scores(): scores = [] y = 1 for

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.