Format Ruby code in Vim
Vimcasts has a useful screencast on this subject that you may be interested in
gg=G
gg => go to start of file
= => apply autoformatting
G => ... to the end of file
Bad formatting ruby code in Vim
To get Ruby indenting working you need to provide indenting configuration. Vim itself is not able to indent Ruby code, you could set the indentexpr variable to some similar language (like basic), but you wont be happy with results. Check your smartindent and indentexpr variables:
:set si?
:set indentexpr?
In my case they are set:
nosmartindent
indentexpr=GetRubyIndent()
The best way to configure vim for ruby is to use vim-ruby plugin: https://github.com/vim-ruby/vim-ruby
How to Autoindent Ruby source code in Vim
vimfiles includes ruby code smart indention and a lot of other useful things
ruby code is automatically formatted like
class Foo
def bar
if xxx
blah
else
blahblah
end
barfoo
barfoo
end
end
How do I reformat ruby code from the command line?
This should be a task of your editor. In vim (configured for Ruby), just press gg=G
xD
A Ruby script that does it is available at: http://www.arachnoid.com/ruby/rbeautify.rb.html
Ruby syntax checking in vim
awesome_person is right, ":w !ruby -c"
will do. To make it more convenient, add this line in your ~/.vimrc:
autocmd FileType ruby map <F9> :w<CR>:!ruby -c %<CR>
Then the syntax gets checked on pressing F9 key.
How do I auto format Ruby or .erb files in VS Code?
You can set format associations in VSCode, so .erb files would be treated like .html.
Go to File->Preferences->Settings->Click ... in top right corner->Open settings.json
Then add this piece of code to your settings.json
"files.associations": {
"*.html.erb": "html"
}
This is how I solved this problem. It will remove some of the code highlights but will autoformat HTML templates like an HTML document.
Vim Ruby hash indentation
try http://github.com/vim-ruby/vim-ruby
seems to be a comprehensive ruby support for vim.
Ruby Source code Auto-Formatter
There is RuboCop:
gem install rubocop
rubocop -a
It will also fix all the common mistakes.
Related Topics
Activerecord Objects in Hashes Aren't Garbage Collected -- a Bug or a Sort of Caching Feature
How to Check If a Number Is Included in a Range (In One Statement)
Don't Create View Folder on Rails Generate Controller
Rails: Ensure Only One Boolean Field Is Set to True at a Time
Deleting Table from Schema - Rails
Adding a Method to Built-In Class in Rails App
How to Attach a Message to Rspec Check
Rails 4.2 - Sidekiq Not Sending Emails in Development
Does Scala Scale Better Than Other Jvm Languages
Rbenv Build Failed on Ubuntu 14.04
Using a Ruby Script to Login to a Website via Https
Best Practice About Empty Belongs_To Association
Using Ruby's Optionparser to Parse Sub-Commands
Namespacing Thor Commands in a Standalone Ruby Executable
How to Read a Barcode from an Image
Converting an Array of Keys and an Array of Values into a Hash in Ruby