Skip to content

Instantly share code, notes, and snippets.

@srcrip
Created February 1, 2021 22:45
Show Gist options
  • Save srcrip/8c50eb1556ae8809f379eb3e17aca1f1 to your computer and use it in GitHub Desktop.
Save srcrip/8c50eb1556ae8809f379eb3e17aca1f1 to your computer and use it in GitHub Desktop.
Pointless method for turning arrays into hashes based on their positional index
# Converts an array like:
# ["M", nil, "G", nil, "G"]
# to:
# {0=>"M", 1=>nil, 2=>"G", 3=>nil, 4=>"G"}
def to_indexed_hash(arr)
arr.each_with_object({}).with_index { |(el, acc), index| acc[index] = el }
end
# Is this pointless? Probably. I thought it was interesting though.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment