Skip to content

Instantly share code, notes, and snippets.

@karlosmid
Last active February 11, 2022 11:41
Show Gist options
  • Save karlosmid/0680444e8581a57784974f84882fe152 to your computer and use it in GitHub Desktop.
Save karlosmid/0680444e8581a57784974f84882fe152 to your computer and use it in GitHub Desktop.
birthdays manipulation
def reverse_birthdays birthdays
result = {}
birthdays.map { |day,names| names.map { |name| result[name] = day } }
result
end
actual = {"2019-01-01"=>["karlo01", "karlo02"], "2019-01-02"=>["ben01", "ben02"]}
expected = {"karlo01"=>"2019-01-01", "karlo02"=>"2019-01-01", "ben01"=>"2019-01-02", "ben02"=>"2019-01-02"}
expected == reverse_birthdays(actual)
@zeljkofilipin
Copy link

Hash#invert might also be interesting solution, but it doesn't work for this example:

actual.invert
=> {["karlo01", "karlo02"]=>"2019-01-01", ["ben01", "ben02"]=>"2019-01-02"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment