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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:17:41+00:00 2026-06-15T20:17:41+00:00

Say I have many arrays of same size, with some elements, for example one

  • 0

Say I have many arrays of same size, with some elements, for example one of those could be:

arr = ['a', 'b', 'c', 'd']

I want to call a different function on each element, depending on its index, so I could do this:

arr.each_with_index do do |el, idx| 
  case idx
  when 0
    functionA el
  when 1
    functionB el
  # and so on..
  end
end

Is there any shorter way to do this? Something similar to assigning to variables:

a, b, c, d = arr

but I want to call functions, instead of doing assignment.

  • 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-15T20:17:42+00:00Added an answer on June 15, 2026 at 8:17 pm

    You could prepare an array of funcs and then zip the two together. Something like this:

    # stabby lambdas instead of methods. Because methods aren't objects.
    func_a = ->(el) { puts "1 for #{el}" }
    func_b = ->(el) { puts "2 for #{el}" }
    func_c = ->(el) { puts "3 for #{el}" }
    
    
    arr = ['a', 'b', 'c', 'd']
    funcs = [func_a, func_b, func_c, func_a] # reusing func_a
    
    arr.zip(funcs) {|a, f| f.call(a) }
    
    # >> 1 for a
    # >> 2 for b
    # >> 3 for c
    # >> 1 for d
    

    But my methods are methods, not lambdas…

    Here’s a trick how to turn methods to proc objects, so that you can put them in array and call later. Be wary, though, that those calls are more expensive as compared to regular methods (shouldn’t matter, unless you’re already squeezing cycles out of your CPU)

    def func_a el; puts "1 for #{el}"; end
    def func_b el; puts "2 for #{el}"; end
    def func_c el; puts "3 for #{el}"; end
    
    
    arr = ['a', 'b', 'c', 'd']
    funcs = [method(:func_a), method(:func_b), method(:func_c), method(:func_a)] # reusing func_a
    
    arr.zip(funcs) {|a, f| f.call(a) }
    
    # >> 1 for a
    # >> 2 for b
    # >> 3 for c
    # >> 1 for d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a Customers table and an Orders table with a one-to-many
I have a program that will store many instances of one class, let's say
Say that I have many blogs, and each blog has_many posts. These are stored
I have many tables, say T1, T2, T3. Every table has a column named
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
Let's say I have a schema where User has many Alerts (many to many).
for clarity lets say we have students and classes, its a many to many
I'm trying to compile many files using the Compiler API. Say I have a
So, let's say I have the longitude/latitude or address of many buildings I can
I have a situation where I have two arrays and I use one array(A)

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.