The New Hash Syntax in Ruby
Tags: ruby
Новый синтаксис хешей в Руби
Новий синтаксис хешів в Рубі
| ← Godaddy On Tonight’s Super Bowl Commercials: Registrations Up 466% Over Last Year; Traffic Doubled & Now Over 45 Million Domains | If programming languages were cars... → |
Starting from Ruby 1.9 the new syntax for hashes was provided. So, if before we had hashes like this:
Hide code highlighting
It's now converted to:
Hide code highlighting
I personally found the new way to use hashes much prettier and try to use it as much as possible in my Ruby projects. Some of the projects that I'm working on right now are still full of the old style syntax. Solution that I've found is to write a converter that will change the hash syntax all over the project from old to a new modern one. That is how the syntax_fix gem was born, you can reach it on Github https://github.com/HeeL/syntax_fix. It's can be also accessed on RubyGems https://rubygems.org/gems/syntax_fix
To install it simply run:
Hide code highlighting
After installation will be completed "syntax_fix" command will be available for you. You can now run it without options in the root of your project and it will check and rewrite all files with the new syntax (including nested files and directories). You can use options -v or --verbose to make it be verbose about the actions. With verbose option on it will show you in a console what files were affected.
That's pretty much it, everything is simple. Originally, the idea of this gem was to change not only hashes, but also lambdas, cases and so on. If you feel like adding any of those features or fixing bugs, you are more then welcome to fork it on Github https://github.com/HeeL/syntax_fix
Hide code highlighting
1 | {:key => 'value', :key2 => 'value2'} |
It's now converted to:
Hide code highlighting
1 | {key: 'value', key2: 'value2'} |
I personally found the new way to use hashes much prettier and try to use it as much as possible in my Ruby projects. Some of the projects that I'm working on right now are still full of the old style syntax. Solution that I've found is to write a converter that will change the hash syntax all over the project from old to a new modern one. That is how the syntax_fix gem was born, you can reach it on Github https://github.com/HeeL/syntax_fix. It's can be also accessed on RubyGems https://rubygems.org/gems/syntax_fix
To install it simply run:
Hide code highlighting
1 | gem install syntax_fix |
After installation will be completed "syntax_fix" command will be available for you. You can now run it without options in the root of your project and it will check and rewrite all files with the new syntax (including nested files and directories). You can use options -v or --verbose to make it be verbose about the actions. With verbose option on it will show you in a console what files were affected.
That's pretty much it, everything is simple. Originally, the idea of this gem was to change not only hashes, but also lambdas, cases and so on. If you feel like adding any of those features or fixing bugs, you are more then welcome to fork it on Github https://github.com/HeeL/syntax_fix
Rating:




<< Please, rate this article