rails:自定义重新加载!检查语法

时间:2015-11-02 11:53:20

标签: ruby-on-rails ruby rails-console

我想自定义rails console reload!命令,这样在重新加载之后它还会检查语法,例如与ruby -c

Caillou的回答后,我尝试将以下内容添加到#reload! railties-4.2.2/lib/rails/console/app.rb

# you may want to include other dirs too
files = Dir[Rails.root + 'lib/**/*.rb']
files.each {|f| `ruby -c #{f}`}

但是在检查使用github中的gems的文件时出现错误,例如

bundler-1.10.6/lib/bundler/source/git.rb:191:in `rescue in load_spec_files': git://github.com/josevalim/inherited_resources.git (at master) is not yet checked out. Run `bundle install` first. (Bundler::GitError)

但是,在shell上对同一文件运行ruby -c会打印syntax OK

2 个答案:

答案 0 :(得分:1)

我按#reload! railties-4.2.2/lib/rails/console/app.rb# you may want to include other dirs too files = Dir[Rails.root + 'lib/**/*.rb'] + Dir[Rails.root + 'app/**/*.rb'] # fix for git mergetool files.reject!{ |f| f =~ /_(BACKUP|BASE|REMOTE|LOCAL)_/} files.each{ |f| RubyVM::InstructionSequence.compile_file(f) } 添加了以下内容:here

master

答案 1 :(得分:0)

您可以找到托管reload!方法的类,如下所示:

method(:reload!)
=> #<Method: Object(Rails::ConsoleMethods)#reload!> 

我想你应该重新打开Rails::ConsoleMethods类并覆盖reload!方法来添加你需要的行为。但是我自己没有成功,也许其他人可以将他的知识添加到答案中。

这是我试过的:

class Rails::ConsoleMethods
  def reload!
    super
    puts "test"
  end
end

当我使用reload!时,它不会打印我的“test”字符串。我不知道出了什么问题,但我几乎可以肯定这就是你需要做的事情。