How do I simplify pushing multiple values into an array in Ruby?

notaceo

How would you improve this:

time = Time.now
    @time = []
    @time.push(
               (time-1.week).strftime("%m-%d"),
               (time-6.days).strftime("%m-%d"),
               (time-5.days).strftime("%m-%d"),
               (time-4.days).strftime("%m-%d"),
               (time-3.days).strftime("%m-%d"),
               (time-2.days).strftime("%m-%d"),
               (time-1.day).strftime("%m-%d"),
               (time).strftime("%m-%d")
              )

I'm trying out some of the suggestions below:

time = Time.now
    iterations = 1000
    Benchmark.bm do |bm|
      bm.report do
         iterations.times do
            @time = 7.downto(0).map { |v| (time - v.days).strftime("%m-%d") }
          end
      end
      bm.report do
       iterations.times do
          @time = []
          @time.push(
               (time-1.week).strftime("%m-%d"),
               (time-6.days).strftime("%m-%d"),
               (time-5.days).strftime("%m-%d"),
               (time-4.days).strftime("%m-%d"),
               (time-3.days).strftime("%m-%d"),
               (time-2.days).strftime("%m-%d"),
               (time-1.day).strftime("%m-%d"),
               (time).strftime("%m-%d")
              )
       end
      end
     end

       user     system      total        real
   0.350000   0.960000   1.310000 (  1.310054)
   0.310000   0.840000   1.150000 (  1.156484)

downto is demonstrably slower than my method.

The next test used the method:

@time = (0..7).map { |x| (time - x.days).strftime("%m-%d") }.reverse

1000 iterations

       user     system      total        real
   0.340000   0.980000   1.320000 (  1.321518)
   0.300000   0.840000   1.140000 (  1.149759)

5000 iterations

       user     system      total        real
   1.720000   4.800000   6.520000 (  6.545335)
   1.530000   4.180000   5.710000 (  5.712035)

I'm having a hard time wrapping my head around this without looking at both downto and map in the Ruby core, but in both cases my more elongated method of writing this responds faster than the more simplified methods (I like the answers below much more from a readability standpoint). Please shed some light on my tests if I'm doing it wrong. I expected map to blow my way out of the water.

UPDATED FOR STEFAN'S ANSWER

So I see Stefan's answer below and throw it in the tester:

       user     system      total        real
   0.040000   0.000000   0.040000 (  0.035976)
   1.520000   4.180000   5.700000 (  5.704401)

Holy crap! 5000 iterations and it absolutely destroys my method.

Because he accurately points out that I'm only interested in Dates, I decide to change my own method from Time.now to Date.today and test it:

       user     system      total        real
   0.090000   0.000000   0.090000 (  0.085940)
   0.390000   0.000000   0.390000 (  0.398143)

It's somewhat weird that in the first test Stefan's method clocks in at 0.0359 and in the second clocks at 0.0859, more than double, but it's still hundredths of a second over 5000 iterations - so I think I'm deep into splitting hairs territory here.

Nevertheless - Stefan's way obliterates my own method - so I'm giving him the check mark.

Stefan

Since you're only interested in dates, you could use a Range of Date instances (dates increment in 1-day steps):

today = Date.today
(today-7..today).map { |date| date.strftime("%m-%d") }
#=> ["04-24", "04-25", "04-26", "04-27", "04-28", "04-29", "04-30", "05-01"]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I simplify pushing multiple values into an array in Ruby?

From Dev

How do I simplify a conditional with multiple or statements?

From Dev

How to simplify array extraction values

From Dev

How do i simplify css with multiple id's

From Dev

Google Script to watch multiple sheets: how do I simplify this script?

From Dev

How do I set multiple array values by index in Scala?

From Dev

How do I sort an array of structs by multiple values?

From Dev

How do I store multiple values in an array in Android?

From Dev

How do I set multiple array values by index in Scala?

From Dev

How can I simplify multiple "||"?

From Dev

How do I simplify this jQuery?

From Dev

How do I simplify this expression?

From Dev

How can I simplify too many AND/OR in ruby

From Dev

How can I iterate through an object array and add matching values while pushing new values

From Dev

How do I get multiple values from MySQL Relational Database tables using Ruby on Rails?

From Dev

Pushing max hash key-values into an array in Ruby?

From Dev

Pushing to an Array in Ruby on Rails

From Dev

Ruby pushing strings to an array

From Dev

Ruby - Pushing Hash to Array

From Dev

Ruby/Rails: How do I convert a keys-values string(with array value included) to valid hash

From Dev

How do I convert a float into an array in ruby?

From Dev

How do I format an array of objects with ruby?

From Dev

pushing values to array nodejs

From Dev

Pushing array values by 1

From Dev

Pushing values into array of in php

From Dev

Array pushing values not as strings

From Dev

How can I simplify a nested php array?

From Dev

Creating array from query results and pushing multiple values to associated key

From Dev

How do I set select multiple values?

Related Related

  1. 1

    How do I simplify pushing multiple values into an array in Ruby?

  2. 2

    How do I simplify a conditional with multiple or statements?

  3. 3

    How to simplify array extraction values

  4. 4

    How do i simplify css with multiple id's

  5. 5

    Google Script to watch multiple sheets: how do I simplify this script?

  6. 6

    How do I set multiple array values by index in Scala?

  7. 7

    How do I sort an array of structs by multiple values?

  8. 8

    How do I store multiple values in an array in Android?

  9. 9

    How do I set multiple array values by index in Scala?

  10. 10

    How can I simplify multiple "||"?

  11. 11

    How do I simplify this jQuery?

  12. 12

    How do I simplify this expression?

  13. 13

    How can I simplify too many AND/OR in ruby

  14. 14

    How can I iterate through an object array and add matching values while pushing new values

  15. 15

    How do I get multiple values from MySQL Relational Database tables using Ruby on Rails?

  16. 16

    Pushing max hash key-values into an array in Ruby?

  17. 17

    Pushing to an Array in Ruby on Rails

  18. 18

    Ruby pushing strings to an array

  19. 19

    Ruby - Pushing Hash to Array

  20. 20

    Ruby/Rails: How do I convert a keys-values string(with array value included) to valid hash

  21. 21

    How do I convert a float into an array in ruby?

  22. 22

    How do I format an array of objects with ruby?

  23. 23

    pushing values to array nodejs

  24. 24

    Pushing array values by 1

  25. 25

    Pushing values into array of in php

  26. 26

    Array pushing values not as strings

  27. 27

    How can I simplify a nested php array?

  28. 28

    Creating array from query results and pushing multiple values to associated key

  29. 29

    How do I set select multiple values?

HotTag

Archive