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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:39:50+00:00 2026-05-15T12:39:50+00:00

I have an array of Elements, and each element has a property :image. I

  • 0

I have an array of Elements, and each element has a property :image.

I would like an array of :images, so whats the quickest and least expensive way to achieve this. Is it just iteration over the array and push each element into a new array, something like this:

images = []
elements.each {|element| images << element.image}
  • 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-15T12:39:51+00:00Added an answer on May 15, 2026 at 12:39 pm

    You can use the Benchmark module to test these sorts of things. I ran @sepp2k’s version against your original code like so:

    require 'benchmark'
    
    class Element
      attr_accessor :image
    
      def initialize(image)
        @image = image
      end
    end
    
    elements = Array.new(500) {|index| Element.new(index)}
    
    n = 10000
    
    Benchmark.bm do |x|
      x.report do
        n.times do
          # Globalkeith's version
          image = []
          elements.each {|element| image << element.image}
        end
      end
      # sepp2k's version
      x.report { n.times do elements.map {|element| element.image} end }
    end
    

    The output on my machine was consistently (after more than 3 runs) very close to this:

       user     system      total        real
    2.140000   0.000000   2.140000 (  2.143290)
    1.420000   0.010000   1.430000 (  1.422651)
    

    Thus demonstrating that map is significantly faster than manually appending to an array when the array is somewhat large and the operation is performed many times.

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

Sidebar

Related Questions

Hey I have an array where each element is a hash containing a few
Suppose I have an array that mimics a database table. Each array element represents
I have an array of structs and I intend to pass each element of
I have a two dimensional JSON array where each element contains several attributes. The
I have a multidimensional array of bools, with each element set to true: var
I have to load an array of doubles from a file, multiply each element
I have an object, tree . tree has a property, tree.elements . tree.elements is
I have an array char[] Select = {'A','B','C','D','E','F','G','H','I','J'} and each element in this array
I have an array, whose size is 256. This array has elements which are
In my code, I have an array that has about 1000 elements : Object[]

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.