[Ruby] I want to reverse the order of the hash table

Introduction

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.

manner

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.

Afterword

"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

[Ruby] I want to reverse the order of the hash table
[Ruby] I want to extract only the value of the hash and only the key
I want to change the value of Attribute in Selenium of Ruby
I want to output the day of the week
I want to var_dump the contents of the intent
I want to get the value in Ruby
[Ruby] I want to display posted items in order of newest date
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
I want to return the scroll position of UITableView!
About the behavior of ruby Hash # ==
[Ruby] I want to make a program that displays today's day of the week!
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I tried to summarize the basic grammar of Ruby briefly
[Ruby] How to retrieve the contents of a double hash
I want to narrow down the display of docker ps
I want to temporarily disable the swipe gesture of UIPageViewController
I want to fetch another association of the parent model from the intermediate table with has_many
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I tried to solve the problem of "multi-stage selection" with Ruby
I want to understand the flow of Spring processing request parameters
The story of Collectors.groupingBy that I want to keep for posterity
Want to know what Ruby n is the power of 2? (Power judgment of 2)
I want to control the default error message of Spring Boot
[Ruby] I want to output only the odd-numbered characters in the character string
I want to display the number of orders for today using datetime.
I want to know the JSP of the open portlet when developing Liferay
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I tried to make full use of the CPU core in Ruby
I want to get the field name of the [Java] field. (Old tale tone)
I want you to use Enum # name () for the Key of SharedPreference
How to find the cause of the Ruby error
I want to sort by tab delimited by ruby
I want to use arrow notation in Ruby
The story of introducing Ajax communication to ruby
[Ruby] I want to do a method jump!
[Ruby] Code to display the day of the week
I want to truncate after the decimal point
I checked the number of taxis with Ruby
Use hashes well in Ruby to calculate the total amount of an order
[RxSwift] I want to deepen my understanding by following the definition of Observable
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to control the start / stop of servers and databases with Alexa
I want to separate the handling of call results according to the API caller (call trigger)
I want to see the contents of Request without saying four or five
I want to recursively get the superclass and interface of a certain class
From the introduction of devise to the creation of the users table
I want to call a method of another class
[Java] I want to calculate the difference from the date
I want to embed any TraceId in the log
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
[Ruby] How to find the sum of each digit
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I want to dark mode with the SWT app
[Rails] How to change the column name of the table
I want to stop snake case in table definition
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method