It seems like the hardest part of learning a language is knowing its tricks. In Ruby, one of these tricks is turning an array into a hash. So let's get started!
Suppose you have the following array:
1
|
|
This array seems to be assigning a level of elegance to the item before it, so a hash may be better suited for it. You could iterate through the array and assign the values into a hash, however Ruby provides an easier way to accomplish this:
1 2 |
|
That's it! Hope you enjoyed this short but sweet refactor tip!