Prettier Ruby

Prettier for Ruby

Gitter GitHub Actions NPM Version code style: prettier Follow+Prettier+on+Twitter

`@prettier/plugin-ruby` is a [prettier](https://prettier.io/) plugin for the Ruby programming language and its ecosystem. `prettier` is an opinionated code formatter that supports multiple languages and integrates with most editors. The idea is to eliminate discussions of style in code review and allow developers to get back to thinking about code design instead. For example, the below [code segment](http://www.rubyinside.com/advent2006/4-ruby-obfuscation.html): ```ruby d=[30644250780,9003106878, 30636278846,66641217692,4501790980, 671_24_603036,131_61973916,66_606629_920, 30642677916,30643069058];a,s=[],$*[0] s.each_byte{|b|a<<("%036b"%d[b. chr.to_i]).scan(/\d{6}/)} a.transpose.each{ |a| a.join.each_byte{\ |i|print i==49?\ ($*[1]||"#")\ :32.chr} puts } ``` when run through `@prettier/plugin-ruby` will generate: ```ruby d = [ 30_644_250_780, 9_003_106_878, 30_636_278_846, 66_641_217_692, 4_501_790_980, 671_24_603036, 131_61973916, 66_606629_920, 30_642_677_916, 30_643_069_058 ] a, s = [], $*[0] s.each_byte { |b| a << ('%036b' % d[b.chr.to_i]).scan(/\d{6}/) } a.transpose.each do |a| a.join.each_byte { |i| print i == 49 ? ($*[1] || '#') : 32.chr } puts end ``` ## Getting started To run `prettier` with the Ruby plugin, you're going to need [`ruby`](https://www.ruby-lang.org/en/documentation/installation/) (version `2.5` or newer) and [`node`](https://nodejs.org/en/download/) (version `8.3` or newer). If you're integrating with a project that is not already using `prettier`, you should use the Ruby gem. Otherwise you can use the `npm` package directly. Note that currently the editor integrations work best with the `npm` package, as most of the major editor plugins expect a `node_modules` directory. You can get them to work with the Ruby gem, but it requires manually configuring the paths. ### Ruby gem Add this line to your application's Gemfile: ```ruby gem 'prettier' ``` And then execute: ```bash bundle ``` Or install it yourself as: ```bash gem install prettier ``` The `rbprettier` executable is now installed and ready for use: ```bash bundle exec rbprettier --write '**/*.rb' ``` ### `npm` package If you're using the `npm` CLI, then add the plugin by: ```bash npm install --save-dev prettier @prettier/plugin-ruby ``` Or if you're using `yarn`, then add the plugin by: ```bash yarn add --dev prettier @prettier/plugin-ruby ``` The `prettier` executable is now installed and ready for use: ```bash ./node_modules/.bin/prettier --write '**/*.rb' ``` ## Configuration Below are the options (from [`src/ruby.js`](src/ruby.js)) that `@prettier/plugin-ruby` currently supports: | API Option | CLI Option | Default | Description | | ------------------ | ---------------------- | :-----: | ------------------------------------------------------------------------------------------------------------------------------------ | | `printWidth` | `--print-width` | `80` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#print-width)). | | `requirePragma` | `--require-pragma` | `false` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#require-pragma)). | | `rubyArrayLiteral` | `--ruby-array-literal` | `true` | When possible, favor the use of string and symbol array literals. | | `rubyHashLabel` | `--ruby-hash-label` | `true` | When possible, uses the shortened hash key syntax, as opposed to hash rockets. | | `rubyModifier` | `--ruby-modifier` | `true` | When it fits on one line, allows while and until statements to use the modifier form. | | `rubySingleQuote` | `--ruby-single-quote` | `true` | When double quotes are not necessary for interpolation, prefers the use of single quotes for string literals. | | `rubyToProc` | `--ruby-to-proc` | `false` | When possible, convert blocks to the more concise `Symbol#to_proc` syntax. | | `tabWidth` | `--tab-width` | `2` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#tab-width)). | | `trailingComma` | `--trailing-comma` | `"es5"` | Same as in Prettier ([see prettier docs](https://prettier.io/docs/en/options.html#trailing-comma)). `"es5"` is equivalent to `true`. | Any of these can be added to your existing [prettier configuration file](https://prettier.io/docs/en/configuration.html). For example: ```json { "rubySingleQuote": false } ``` Or, they can be passed to `prettier` as arguments: ```bash prettier --ruby-single-quote false --write '**/*.rb' ``` ## Contributing Check out our [contributing guide](CONTRIBUTING.md). Bug reports and pull requests are welcome on GitHub at https://github.com/prettier/plugin-ruby. ### Code Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

Kevin Deisz

πŸ’» πŸ“– ⚠️ πŸ›

Alan Foster

πŸ’» πŸ“– ⚠️ πŸ›

johnschoeman

⚠️

Aaron Jensen

πŸ“–

Cameron Bothner

πŸ’»

localhost.dev

πŸ› πŸ’»

David Buchan-Swanson

πŸ› πŸ’»

Jordan Pickwell

πŸ›

Josh Justice

πŸ›

xipgroc

πŸ›

Gregoire Lejeune

πŸ›

Pete Van Klaveren

πŸ›

meleyal

πŸ“–

Lipis

πŸ“–

Jan Piotrowski

πŸ“–

Andy Waite

πŸ“– πŸ›

Jonathan Viney

πŸ›

acrewdson

πŸ›

Louis Orleans

πŸ›

Colton Voege

πŸ›

Stefan Kracht

πŸ›

jakeprime

πŸ›

Mario Mainz

πŸ›

CL Web Developers

πŸ›

github0013

πŸ›

James Costian

πŸ›

Joe Jackson

πŸ›

Max Albrecht

πŸ›

Matt

πŸ› πŸ’»

Vesa Laakso

πŸ“–

jrdioko

πŸ›

gin0606

πŸ› πŸ’»

Tobyn

πŸ’»

Ian Ker-Seymer

πŸ’»

Richard Huang

πŸ’»

Patrick Ellis

πŸ›

Peter De Berdt

πŸ›

Chris Hafley

πŸ›

Thomas FrΓΌtel

πŸ›

Alex Serban

πŸ’»

Stephanie

πŸ’»

Shay Davidson

πŸ›

Ryan Hunter

πŸ’»

Jan Klimo

πŸ’»

Rick Sullivan

πŸ› πŸ’»

Stephen O'Brien

πŸ›

Nimish Mehta

πŸ› πŸ’»

Josh Bielick

πŸ› πŸ’»

RomΓ‘n CoitiΓ±o

πŸ›

Matt McNeil

πŸ›

John Snyder

πŸ›

Jacek Jakubik

πŸ›

Yukito Ito

πŸ› πŸ’»

Karan Mandal

πŸ›
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome! ### Financial You can support `prettier/plugin-ruby` [on OpenCollective](https://opencollective.com/prettier-ruby/contribute). Your organization's logo will show up here with a link to your website. ## License The package is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).