I’ve been using flycheck-mode in emacs for about a month now. Out of the box, it does a great job checking the syntax of my code. As I type, it automatically highlights syntax errors and warnings - it looks something like this:

1
2
3
4
5
  class Contrived
    def sample1()
 ?    puts ("this is a contrived example")
    end
  end

flycheck-mode notices that there is a space between the puts statement and the left parenthesis, so it highlights the line. When you cursor over the underlined text, it describes the nature of the warning. Errors are similar, except they are underlined red.

Under the hood, flymake-mode runs the file through the standard ruby interpreter (or jruby interpreter if that’s what you are using) to get its results. However - we can do much better. If you’ve installed the rubocop gem, flycheck-mode will use that for it’s syntax checking.

gem install rubocop

Here’s the code with rubocop installed:

1
2
3
4
5
 ? class Contrived
 ?  def sample1()
 ?    puts ("this is a contrived example")
    end
  end

The rubocop parser is much more picky. It warns that the class doesn’t have a top-level documentation comment, the parenthesis in the method declaration are unnecessary, and double-quote strings aren’t needed when there is no string interpolation. If we run rubocop from the terminal, we get this:

  > rubocop
  Inspecting 1 file
  C

  Offences:

  sad.rb:1:1: C: Missing top-level class documentation comment.
  class Contrived
  ^^^^^
  sad.rb:2:14: C: Omit the parentheses in defs when the method doesn't accept any arguments.
    def sample1()
           ^
  sad.rb:3:11: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      puts ("this is a contribed example")
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  1 file inspected, 3 offences detected

Rubocop can be rather heavy-handed - Maybe you don’t care if your strings are double-quoted. Fortunately, you can control this by creating a .rubocop.yml file in your project’s root directory. You can direct it to create a config file with all it’s switches turned off:

  rubocop --auto-gen-config

This will generate the file rubocop-todo.yml. Rename this file to .rubocop.yml and change the settings to your liking.

Both flycheck and rubocop are under development and are surrounded by active communities. Learn more about flycheck-mode here: http://flycheck.lunaryorn.com and rubocop here: http://batsov.com/rubocop