When I wind up with code that is not well formatted, the = command comes in handy. This command runs applies a formatter to your motion or visual selection. So two handy sequences of commands I keep at hand are gg=G and =%. The later takes advantage of the matchit plugin which enhances % to match a broad array of open close blocks like those found in the Ruby programming language.

def read_something(filename)
      open(filename) do |fh|
fh.each_line do |line|
         puts line
end
      end
      end

Applying gg=G formats the entire file:

def read_something(filename)
  open(filename) do |fh|
    fh.each_line do |line|
      puts line
    end
  end
end

Note that reformatting can be performed on a visual selection by hitting = after selecting with V

Lastly, the command that performs formatting can be set using :set equalprg. See :help = and :help equalprg for more details.