Skip to content

Instantly share code, notes, and snippets.

@3limin4t0r
Last active January 9, 2020 10:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3limin4t0r/8d90fda851e43dc80b03353f9542e4f0 to your computer and use it in GitHub Desktop.
Save 3limin4t0r/8d90fda851e43dc80b03353f9542e4f0 to your computer and use it in GitHub Desktop.
months = ["2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"]
monthly_doc_count_for_topic = [
["foo","2019-02-01: 186904","2019-03-01: 196961"],
["bar","2019-01-01: 8876","2019-04-01: 8694"]
]
monthly_doc_count_for_topic.map { |array| array + months }
#=> [
# ["foo", "2019-02-01: 186904", "2019-03-01: 196961", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"],
# ["bar", "2019-01-01: 8876", "2019-04-01: 8694", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"]
# ]
# +monthly_doc_count_for_topic+ should be unchanged, since we
# only used non-mutating methods.
# Your solution also works, but mutates each array inside
# +monthly_doc_count_for_topic+. I've replaced #map with #each
# here. Which only effects the return value, which we aren't
# using here.
monthly_doc_count_for_topic.each do |topic_set|
months.each { |month| topic_set << month }
end
monthly_doc_count_for_topic
#=> [
# ["foo", "2019-02-01: 186904", "2019-03-01: 196961", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"],
# ["bar", "2019-01-01: 8876", "2019-04-01: 8694", "2019-01-01", "2019-02-01", "2019-03-01", "2019-04-01"]
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment