My Knowledge Wiki
Back to my website
Search…
Introduction
My Stack
Sharing
Work
Apps & Tools
DevOps
Awesome Products
Engineering Management
Software Architecture
Talks
Documentation
Machine Learning
Programming Languages
Constructs
Go
Ruby
Ruby on Rails
Debugging
Libraries
Lazy Enumerators
Snippets
Version Control
Education
HTTP
Design
Arabic Content | محتوى عربي
Biology
Powered By
GitBook
Snippets
A couple of snippets for things I have to do a lot but forget how every once in a while
Using
.map
on a Hash
{
a
:
'a'
,
b
:
'b'
}.
map
{
|
k
,
str
|
[
k
,
"%
#{
str
}
%"
]
}.
to_h
Convert an Array of Hashes to a Hash
# I have an array of hashes
array
=
[
{
id
:
1
,
name
:
"test"
}
]
# I want to turn it to a hash, where the key is `id`
array
.
map
{
|
h
|
[
h
[
:id
],
h
]
}.
to_h
#=> { 1 => {:id => 1, :name => "test"} }
Previous
Lazy Enumerators
Next
Version Control
Last modified
2yr ago
Copy link
Outline
Using .map on a Hash
Convert an Array of Hashes to a Hash