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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:25:17+00:00 2026-06-14T16:25:17+00:00

As we know, wo can pass a method to a iterator method by a

  • 0

As we know, wo can pass a method to a iterator method by a &: prefix.
For example:

["a", "b"].map(&:upcase) #=> ["A", "B"] 
def rettwo
  2
end
["a", "b"].map(&:rettwo) #=> [2, 2]

Here is the problem, when I write a method, pass a method with &: prefix to it, I got a error message: “ArgumentError: no receiver given”.
Let me show the code:

def a_simple_method &proc
  puts proc.class # it shows `Proc`
  proc.call
end
def a_iterator_method
  puts yield
end

a_simple_method &:rettwo #=> ArgumentError: no receiver given
a_iterator_method &:rettwo #=> ArgumentError: no receiver given

What do I missing, How the map like method of Array handle it

  • 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-14T16:25:18+00:00Added an answer on June 14, 2026 at 4:25 pm

    Here’s what works. Explanation below.

    class String
      def rettwo
        self + self
      end
    end
    
    def a_simple_method &proc
      proc.call('a')
    end
    
    def a_iterator_method
      yield 'b'
    end
    
    a_simple_method(&:rettwo) # => "aa"
    a_iterator_method(&:rettwo) # => "bb"
    

    The &: construct is called Symbol#to_proc. It turns symbol into a proc. This proc expects a receiver as a first argument. The remaining arguments are used to call the proc. You’re not passing any arguments, hence the “receiver not given” error.

    Here’s a demonstration of additional arguments:

    class String
      def say name
        "#{self} #{name}"
      end
    end
    
    def a_simple_method &proc
      proc.call('hello', 'ruby')
    end
    
    
    a_simple_method(&:say) # => "hello ruby"
    

    Here’s a definition of Symbol#to_proc from some blog post from 2008. Modern Symbol#to_proc seems to be implemented in C, but this can still help the understanding.

    class Symbol
      def to_proc
        Proc.new { |*args| args.shift.__send__(self, *args) }
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know from here that I can pass an ignore option in the jQuery.validate
I know you can pass arguments through the RunWorkerAsync function call when you first
I'm learning javascript. I know we can pass a function to other functions after
I know that I can pass a string as the second parameter to the
I would like to know how can I pass a ruby variable inside an
In Visual Studio Debug part, we can pass arguments. I want to know how
I would like to know how I can do a low-pass filter in opencv
I can't seem to get these Rspec tests to pass. I just don't know
I can pass multiple parameters and at last one block parameter to method. But
I would like to know how I can pass a list with values 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.