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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:56:03+00:00 2026-05-31T06:56:03+00:00

def show a, &b yield a end show 1 {|x| puts x} but without

  • 0
def show a, &b
yield a
end

show 1 {|x| puts x}

but without &b in the definition, the code works too, so I want to know under what circumstance, the &b is required?

  • 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-31T06:56:05+00:00Added an answer on May 31, 2026 at 6:56 am

    A block is normally an anonymous argument to a method. In most cases, you execute the block right there in the method, using yield. There are two cases where yield is not enough:

    • You want to pass block to an another method.

    • You want to convert the block to a Proc.

    In both of these cases you need to refer your block via name. To do that, you attach one more parameter (last arguament always) and use & as prefix to that parameter.

    For first case,

    def math(a, b) 
      yield(a, b)
    end
    
    def teach_math(a, b, &operation) 
      puts "Let's do the math:"
      puts math(a, b, &operation)
    end
    
    
    teach_math(2, 3) {|x, y| x * y}
    
    # Output
    # Let's do the math: 
    # 6
    

    For second case, (when you need to convert to Proc)

    def my_method(&the_proc) 
      the_proc
    end
    
    p = my_method {|name| "Hello, #{name}!" } 
    puts p.class
    puts p.call("Bill")
    
    # Output
    # Proc
    # Hello, Bill!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

s = Proc.new {|x|x*2} def one_arg(x) puts yield(x) end one_arg(5, &s) How does one_arg
If I have a block of code like this: def show @post = Post.find(params[:id])
I guess the subject sounds pretty stupid, so I'll show some code: def foo(**kwargs):
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like
def any? if block_given? method_missing(:any?) { |*block_args| yield(*block_args) } else !empty? end end In
Given a controller method like: def show @model = Model.find(params[:id]) respond_to do |format| format.html
import wx class MainFrame(wx.Frame): def __init__(self,parent,title): wx.Frame.__init__(self, parent, title=title, size=(640,480)) self.mainPanel=DoubleBufferTest(self,-1) self.Show(True) class DoubleBufferTest(wx.Panel):
def update @album = Album.find(params[:id]) if @album.update_attributes(params[:album]) redirect_to(:action=>'list') else render(:action=>'edit') end end A Rails
UPDATE my show function def show @contact = Contact.find(params[:id]) @data = Register.all :include =>
I'd like to implement drag&drop in wxPython that works in similar way that in

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.