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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:43:10+00:00 2026-06-16T01:43:10+00:00

Possible Duplicate: Shorter way to pass every element of an array to a function

  • 0

Possible Duplicate:
Shorter way to pass every element of an array to a function

I know this will work:

def inc(a)
  a+1
end
[1,2,3].map{|a| inc a}

but in Python, I just need to write:

map(inc, [1,2,3])

or

[inc(x) for x in [1,2,3])

I was wondering whether I can skip the steps of making a block in Ruby, and did this:

[1,2,3].map inc
# => ArgumentError: wrong number of arguments (0 for 1)
# from (irb):19:in `inc'

Does anyone have ideas about how to do this?

  • 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-16T01:43:12+00:00Added an answer on June 16, 2026 at 1:43 am

    According to “Passing Methods like Blocks in Ruby“, you can pass a method as a block like so:

    p [1,2,3].map(&method(:inc))
    

    Don’t know if that’s much better than rolling your own block, honestly.

    If your method is defined on the class of the objects you’re using, you could do this:

    # Adding inc to the Integer class in order to relate to the original post.
    class Integer
      def inc
        self + 1
      end
    end
    
    p [1,2,3].map(&:inc)
    

    In that case, Ruby will interpret the symbol as an instance method name and attempt to call the method on that object.


    The reason you can pass a function name as a first-class object in Python, but not in Ruby, is because Ruby allows you to call a method with zero arguments without parentheses. Python’s grammar, since it requires the parentheses, prevents any possible ambiguity between passing in a function name and calling a function with no arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: nth-child for every two table rows I'm trying to work my way
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Easiest way to get file's contents in C I saw this post
Possible Duplicate: When is a Java method name too long? I know this is
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Objective C for Windows iPhone development on Windows Is there any way
Possible Duplicate: When does Endianness become a factor? reading this tuto on endianess, i
Possible Duplicate: Realloc is not resizing array of pointers Can anyone tell me where
Possible Duplicate: How to read Windows loged in username with PHP/IIS I'm work on

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.