有没有快速找到Ruby中缺失的结尾的方法?

Ala*_* H. 16 ruby syntax

语法错误,意外$ end,期待keyword_end

我们都去过那儿!假设有足够的代码改变了,快速浏览一下git diff就不会显而易见,是否有一种简单的方法可以找到丢失的内容end(没有切换到像Python这样的基于缩进的语言)?

FWIW,我使用Sublime Text 2作为我的编辑器.

sun*_*eja 33

如果您使用的是Ruby 1.9,请-w在运行ruby程序时尝试使用该标志.

# t.rb
class Example
  def meth1
    if Time.now.hours > 12
      puts "Afternoon"
  end
  def meth2
    # ...
  end
end

ruby t.rb  
=> t.rb:10: syntax error, unexpected $end, expecting keyword_end

ruby -w t.rb
=> t.rb:5: warning: mismatched indentations at 'end' with 'if' at 3
   t.rb:9: warning: mismatched indentations at 'end' with 'def' at 2
   t.rb:10: syntax error, unexpected $end, expecting keyword_end
Run Code Online (Sandbox Code Playgroud)

资源:

http://pragdave.blogs.pragprog.com/pragdave/2008/12/ruby-19-can-check-your-indentation.html

  • 这只有在您使用一致缩进时才有效吗?(1) (2认同)
  • @ augustin-riedinger我发现,至少在某些情况下,您可以使用-w选项从命令行运行Rails类,它会在Rails细节陷入困境之前发现不匹配.例如ruby -w app/controllers/foo_controller.rb. (2认同)