For example, if you have a hashtable like reputation = {very_bad:'worst', bad:'bad', good:'good', very_good:'best'}
, the order is {very_good:'best'. , good:'good', bad:'bad', very_bad:'worst'}
I want to line up from the back.
pry(main)> reputation.to_a.reverse.to_h
#=> {very_good: 'Best', good: 'good', bad: 'bad', very_bad: 'worst'}
Only this.
For the time being, I would like to use a method called reverse
that reverses the order of the array, but reputation
is hash, so it cannot be used. So, once you convert hash to an array with to_a
and reverse
, it will remain an array as it is, so you can restore it with to_h
.
"Using gem's ʻenum_help`, I want to process the radio button display in Japanese notation, internally int type. I want to assign the one with the lowest rating to the one with the lowest number, but the radio button display has the highest rating I wanted to display from. " Normally, when using an enum, the value should be a number, so it's easy to sort, but if you use enum_help, you will pass a hash like the one described above, so it became "How do you sort ...".
I don't think it will be an article, but if you are a beginner like me, even if you use to_s
and to_i
, you may get stuck with to_a
because there are few opportunities to use it. I hope it helps someone even a little.
Recommended Posts