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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:08:22+00:00 2026-06-18T05:08:22+00:00

I am building a hash into an array and then pushing each one into

  • 0

I am building a hash into an array and then pushing each one into one big array. This works fine (I’m using Gmaps4Rails) but I’m wondering if there’s a more Ruby way to go about it?

def index   
  @allpoints = []
  @links = Link.find([1, 2, 3])
  @links.each do |link|
    linkpoints = []
    link.link_points.each do |pt|
      linkpoints << { :lat => pt.latitude, :lng => pt.longitude }
    end         
    @allpoints << linkpoints
  end
  @data = @allpoints.to_json    
end

SOLUTION EDIT: (I went with the one below) Shorter than my original, yet good enough readability in my eyes:

def index
  all_points = []
  Link.find([1,2,3]).each do |link|
    all_points << link.link_points.map { |pt| { lat: pt.latitude, lng: pt.longitude } }
  end
  @data = all_points.to_json
end
  • 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-18T05:08:23+00:00Added an answer on June 18, 2026 at 5:08 am

    First: you don’t need to create tons of @variables. The ones you don’t use in your views (local variables to your Controller’s action) should be classic local variables.

    Second: You could refactor your action like this:

    @data = Link.find([1,2,3]).map do |link|
      link.link_points.map{ |pt| {lat: pt.latitude, lng: pt.longitude} }
    end.to_json
    

    Let me know if the code above doesn’t not have the expected behavior and the output.

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

Sidebar

Related Questions

I'm building a Backbone.js app with hash based navigation. It works fine everywhere except
I have a 'hash tree' like this: dat = { 'building' => {'street' =>
I'm a front end guy getting more and more into using sinatra. I'm building
I am reading a postfix mail log file into an array and then looping
Let's say I have a hash like this my %profile = ( building =>
I am building a hash where the keys, associated with scalars, are not necessarily
Building an inventory system. I have lots of products and each product has three
building a site using PHP and MySQL that needs to store a lot of
I'm building a commenting mechanism into an application that allows a programmer/plugin author to
I'm building a multi-step form in rails. It's not javascript driven, so each page

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.