Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order Hash and delete first key-value pair

I have a Hash with timestamp as keys.

hash = {
  "2016-05-31T22:30:58+02:00" => {
          "path" => "/",
        "method" => "GET"
  },
  "2016-05-31T22:31:23+02:00" => {
          "path" => "/tour",
        "method" => "GET"
  },
  "2016-05-31T22:31:05+02:00" => {
          "path" => "/contact_us",
        "method" => "GET"
  }
}

I order the collection and get the first pair like this:

hash.sort_by {|k, _| k}.first.first

But how do I remove it?

The delete method requires you to know the exakt spelling of the key. Of course I could return the key and then use it in the delete method, but I was thinking if there was any more straight forward way?

like image 348
Fellow Stranger Avatar asked May 31 '16 20:05

Fellow Stranger


1 Answers

Also note that shift can be used on the Hash values.

hash.shift

Removes the first key-value pair from the hash. Works on arrays too.

like image 199
Kelsey Hannan Avatar answered Oct 22 '22 13:10

Kelsey Hannan