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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:43:23+00:00 2026-06-06T12:43:23+00:00

I am looking for the Ruby-est way to solve a problem. I have code

  • 0

I am looking for the Ruby-est way to solve a problem.

I have code of a form similar to this in my application:

data = [1,2,3,4]
a = []
b = []
h = {}
data.each do |val|
  h[val] = func1(val)
  a.push func2(val)
  b.push func3(val)
end

This is, of course, a simplification. What I’d like to do, given code like this, is to eliminate lines 2-4 of this snippet, so that I have something like:

 h, a, b = data.some_func{|val|
   # do something
 }

My instinct is that map won’t be enough, but I’m not sure what I need instead. My code works, but it doesn’t look very rubyish. What should I be doing here?

  • 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-06T12:43:25+00:00Added an answer on June 6, 2026 at 12:43 pm

    Maybe it’s not exactly what you’re asking for, but at least it’s readable:

    data = [1,2,3,4]
    h = data.each_with_object({}) { |e, m| m[e] = func1(e) }
    a = data.map &method(:func2)
    b = data.map &method(:func3)
    

    Though, as you said, your example is a simplification of a real task, so it might not work for you. In that case I’d recommend to stay with your original solution.

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

Sidebar

Related Questions

I tried looking through this Ruby documentation , but have no idea why I
Looking for something similar to xerces for parsing an xml file in ruby. I
I'm looking to expand my ruby knowledge beyond scripting, test code and file parsers
While looking over some Ruby code I noticed methods declared with self. prepended to
I am new to ruby and looking for an elegant way to sort the
I'm looking for a method in Ruby which is basically this: io.ready_for_read? I just
I'm looking at some ruby code in rails, which looks like: user.jobs.order('created_at').not_closed.page(5) In ActiveRecord::Base,
I am looking for a ruby code quality checker that could catch or notify
I am looking for a ruby on rails wiki application. which i can download
I am an experienced PHP programmer, but I have been looking into Ruby lately,

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.