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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:53:56+00:00 2026-05-23T17:53:56+00:00

def double(a) a*2 end method_object = method(:double) and here’s my question, how does this

  • 0
def double(a)
 a*2
end

method_object = method(:double)

and here’s my question, how does this code:

[1,3,5,6].map(&method_object)

achieve the same result of

[1,3,5,6].map {|x| method_object.call(x)}

in the Ruby document, the Array#map only can have block, not normal parameter

ary.collect {|item| block } → new_ary ary.map {|item| block } → new_ary ary.collect → an_enumerator ary.map → an_enumerator
  • 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-23T17:53:57+00:00Added an answer on May 23, 2026 at 5:53 pm

    Simply put, the ampersand & is used to “pack / unpack” a method object to a block, so the effect is more-less the same as if you passed the block.

    You can “get” the block that has been passed to your method by:

    def method_with_block(&foo)
      # do something here
      foo.call
      # do something else
    end
    

    This will be similar to calling yield and not declaring &foo as parameter. I think binding might differ between the two approaches, but in most cases the effect is what you would expect (if I am mistaken, please correct).

    Of course, the ampersand works the other way around – if a method expects a block and you have a proc object, you can simply prepend it with &, just as you wrote. To be more precise, & calls to_proc method of the passed object (Rails uses this in a manner similar to described in this entry about to_proc.

    I hope that this answers some of your doubts. In case there is a mistake in what I wrote, feel free to correct it.

    Readings you might find useful:

    • Programming Ruby, essential book
    • Wikibooks contains a section about this topic
    • This simple blog entry shows an example
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this method: public def getXScaleFactor(panelWidth: Int): Double = { return (panelWidth /
module Superpower # instance method def turn_invisible ... end # module method def Superpower.turn_into_toad
I have this in my application_helper.rb : def bbcode(text) # Code snippets text.gsub!(/\[code=?[']?(.*?)[']?\](.*?)\[\/code\]/mis) {
Newbie Scala Question: Say I want to do this [Java code] in Scala: public
Possible Duplicate: What does !! mean in ruby? what is this function doing? def
class A def initialize @x = do_something end def do_something 42 end end How
Consider the following code: def CalcSomething(a): if CalcSomething._cache.has_key(a): return CalcSomething._cache[a] CalcSomething._cache[a] = ReallyCalc(a) return
Given a controller method like: def show @model = Model.find(params[:id]) respond_to do |format| format.html
Here's the very dumb way: def divisorGenerator(n): for i in xrange(1,n/2+1): if n%i ==
I have some Scala code that does something nifty with two different versions of

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.