Ruby array of hashes find values by key [closed] - ruby

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have an array like below
[{500=>{"id"=>1, "name"=>"Hock Tong Bee Pte Ltd/ Corner stone wines",
"created_at"=>Thu, 19 Jan 2017 18:10:35 UTC +00:00, "updated_at"=>Thu,
19 Jan 2017 18:10:35 UTC +00:00, "website"=>nil, "status"=>nil,
"industry_type_id"=>1, "major_category_id"=>1,
"minor_category_id"=>75, "save_as_draft"=>false, "company_id"=>1}},
{600=>{"id"=>2, "name"=>"Continental Wines", "created_at"=>Thu, 19 Jan
2017 18:10:35 UTC +00:00, "updated_at"=>Thu, 19 Jan 2017 18:10:35 UTC
+00:00, "website"=>nil, "status"=>nil, "industry_type_id"=>1, "major_category_id"=>1, "minor_category_id"=>75,
"save_as_draft"=>false, "company_id"=>2}}]
I have key's like 500/600 from this array i want tot get that object.
I need to write one method, I will pass key only that should return appropriate object. Example if I send key is 500 result should be
{"id"=>1, "name"=>"Hock Tong Bee Pte Ltd/ Corner stone wines",
"created_at"=>Thu, 19 Jan 2017 18:10:35 UTC +00:00, "updated_at"=>Thu,
19 Jan 2017 18:10:35 UTC +00:00, "website"=>nil, "status"=>nil,
"industry_type_id"=>1, "major_category_id"=>1,
"minor_category_id"=>75, "save_as_draft"=>false, "company_id"=>1}
Anyone have idea on how to achieve this in Ruby?

I believe simple reduce method should work:
hash = [{500=>{"id"=>1, "name"=>"Hock Tong Bee Pte Ltd/ Corner stone wines", "created_at"=>"Thu, 19 Jan 2017 18:10:35 UTC +00:00", "updated_at"=>"Thu, 19 Jan 2017 18:10:35 UTC +00:00", "website"=>nil, "status"=>nil, "industry_type_id"=>1, "major_category_id"=>1, "minor_category_id"=>75, "save_as_draft"=>false, "company_id"=>1}}, {600=>{"id"=>2, "name"=>"Continental Wines", "created_at"=>"Thu, 19 Jan 2017 18:10:35 UTC +00:00", "updated_at"=>"Thu, 19 Jan 2017 18:10:35 UTC +00:00", "website"=>nil, "status"=>nil, "industry_type_id"=>1, "major_category_id"=>1, "minor_category_id"=>75, "save_as_draft"=>false, "company_id"=>2}}]
your_value = hash.reduce({}) { |h, v| h.merge v }[500]

Related

How to check if a date value inside an hash is bigger than a reference date

I'm writing a validation and I have an hash with this structure
elements.map{ |e| [e.id,e.coverable.published_at] }.to_h
=> {305=>Fri, 17 Apr 2020 15:23:00 CEST +02:00,
306=>Fri, 17 Apr 2020 13:00:00 CEST +02:00,
307=>Fri, 17 Apr 2020 09:20:00 CEST +02:00,
308=>Fri, 17 Apr 2020 12:59:00 CEST +02:00,
309=>Fri, 17 Apr 2020 11:39:00 CEST +02:00}
I have a reference date...
published_at
=> Mon, 04 May 2020 23:51:00 CEST +02:00
I have to check if any of the element has a published_at datetime value bigger than my published_at.
Is there a short way to do that?
Try something like this
elements.any? { |e| e.coverable.published_at > your_published_at }
In case you need the element which passes the condition use find
element = elements.find { |e| e.coverable.published_at > your_published_at }
# if element is not nil such element is present

Get min and max value from this array of hashes

I have an array that contains a hash in each row containing created_at and a value. How do I get the min and max from the array for the value fields?
The array is called - channels_counts_for_history_graph
and
channels_counts_for_history_graph.max[1]
Gives me the max date rather than the max value?
[[Sun, 30 Dec 2018 15:03:55 UTC +00:00, 4305],
[Sun, 30 Dec 2018 15:05:42 UTC +00:00, 4305],
[Mon, 31 Dec 2018 09:24:06 UTC +00:00, 4306],
[Sat, 05 Jan 2019 09:04:50 UTC +00:00, 4308],
[Tue, 01 Jan 2019 11:26:04 UTC +00:00, 4306],
[Wed, 02 Jan 2019 17:24:19 UTC +00:00, 4305]]
Any help appreciated.
Thanks
I suggest using Enumerable#minmax_by to get the min and the max value in just one method call:
array = [['Sun, 30 Dec 2018 15:03:55 UTC +00:00', 4305],['Sun, 30 Dec 2018 15:05:42 UTC +00:00', 4305],['Mon, 31 Dec 2018 09:24:06 UTC +00:00', 4306],['Sat, 05 Jan 2019 09:04:50 UTC +00:00', 4308],['Tue, 01 Jan 2019 11:26:04 UTC +00:00', 4306],['Wed, 02 Jan 2019 17:24:19 UTC +00:00', 4305]]
array.minmax_by(&:last)
#=> [["Sun, 30 Dec 2018 15:03:55 UTC +00:00", 4305], ["Sat, 05 Jan 2019 09:04:50 UTC +00:00", 4308]]
By default when you sort an array sorts by the first element first.
You can reverse the array for the purposes of the sort.
channel_counts_for_history_graph.map(&:reverse).max[0]
I may guess that this is what you were asking for:
[{ created_at: Date.new(2017, 1, 1) }, { created_at: Date.new(2019, 1, 1) }, { created_at: Date.new(2018, 1, 1) }]
.minmax_by { |value| value[:created_at] }

Ruby: Datetime to UTC conversion

I am trying to convert the below date and time combination to UTC
from_date: "2017-06-19",from_time: "14:00"
to_date: "2017-06-19", to_time: "23:00"
Timezone: EDT
I am using below piece of code for conversion
Date.parse(dt).to_datetime + Time.parse(t).utc.seconds_since_midnight.seconds
And it gives the wrong date value for the to_date & to_time combination.
Output:
Date.parse(from_date).to_datetime +
Time.parse(from_time).utc.seconds_since_midnight.seconds
#⇒ **Mon, 19 Jun 2017 18:00:00 +0000**
Date.parse(to_date).to_datetime +
Time.parse(to_time).utc.seconds_since_midnight.seconds
#⇒ **Mon, 19 Jun 2017 03:00:00 +0000**
Above conversion should give "Tue, 20 Jun 2017 03:00:00 +0000" instead.
Below line of codes worked for me:
parsed_date = Time.zone.parse(from_date).strftime('%Y-%m-%d')
parsed_time = Time.zone.parse(from_time).strftime('%T')
Time.parse(parsed_date + ' ' + parsed_time).utc.strftime('%F %T')
require 'time'
from = Time.parse "2017-06-19 14:00 US/Eastern"
=> 2017-06-19 14:00:00 -0400
from.utc
=> 2017-06-19 18:00:00 UTC
to = Time.parse "2017-06-19 23:00 US/Eastern"
=> 2017-06-19 23:00:00 -0400
to.utc
=> 2017-06-20 03:00:00 UTC
Though you can also specify the timezone offset without using the string, doing it this way handles Daylight Savings Time.
I think this is shorter:
from_date = "2017-06-19"
from_time = "14:00"
DateTime.strptime("#{from_date}T#{from_time}ZEDT", "%Y-%m-%dT%H:%MZ%z").utc
=> Mon, 19 Jun 2017 18:00:00 +000
to_date = "2017-06-19"
to_time = "23:00"
DateTime.strptime("#{to_date}T#{to_time}ZEDT", "%Y-%m-%dT%H:%MZ%z").utc
=> Tue, 20 Jun 2017 03:00:00 +0000

DateTime set TimeZone

ubuntu 14.04
ruby 1.9.3-p484
rails 3.2.18
I have a date as a string: 06/20/2015 02:45 AM
d = DateTime.strptime('06/20/2015 02:45 AM', '%m/%d/%Y %I:%M %p').to_time
=> Sat, 20 Jun 2015 02:45:00 UTC
Current TimeZone may be different and placed in Time.zone.
I tried d.to_time.in_time_zone. It gives respectively for PDT and CDT TimeZone:
Fri, 19 Jun 2015 21:45:00 CDT -05:00
Fri, 19 Jun 2015 19:45:00 PDT -07:00
I need to get DateTime object that holds date Sat, 20 Jun 2015 02:45:00 PDT -07:00 for PDT zone or Sat, 20 Jun 2015 02:45:00 CDT -05:00 for CDT zone.
I think it would work:
zone = ActiveSupport::TimeZone.new("Central Time (US & Canada)")
d.to_time.in_time_zone.in_time_zone(zone)
or just
d.to_time.in_time_zone.in_time_zone("Central Time (US & Canada)")
Try this:
#config/application.rb
config.time_zone = 'Central Time (US & Canada)'
config.active_record.default_timezone = :local
Don't forget to restart your server.
I have found the solution. Method DateTime#offset rules:
d = DateTime.strptime('06/20/2015 02:45 AM', '%m/%d/%Y %I:%M %p')
d = d.change(offset: (Time.zone.now.utc_offset / 3600).to_s)

How to detect time in Go?

I need to parse the date_id field from emails headers. However these seem to have slightly different layouts so I've started to build a switch case/block . I'm wondering if that's really the way to fix this issue.
case strings.Contains(h.Headers[bk].Date, "(CEST)"):
layout = "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
case strings.Contains(h.Headers[bk].Date, "(EDT)"):
layout = "Mon, 02 Jan 2006 15:04:05 -0700 (MST)"
default:
layout = "Mon, 02 Jan 2006 15:04:05 -0700"
}
You could use mail.Header and simply call .Date() on it.
Another option is to read the code starting from line 70 and write your own function.

Resources