简体   繁体   English

在Ruby on Rails的IRB上停止加载脚本执行的最佳方法

[英]Best way to stop load script execution in Ruby on Rails's IRB

Looking for the best way to exit a loaded script in Ruby on Rails's IRB. 寻找在Ruby on Rails的IRB上退出已加载脚本的最佳方法。 Kernel.exit & Kernel.abort will pop you out of the IRB session altogether. Kernel.exit和Kernel.abort会让你完全退出IRB会话。 Ideally, this would happen on some conditional criteria. 理想情况下,这会在某些条件标准上发生。

My script/my_script.rb: 我的脚本/ my_script.rb:

puts "My Script is running!"
# if some criteria, stop execution of this script without stoping IRB
puts "This will never happen"

Ideal results: 理想的结果:

$ rails console
irb(main):001:0> load 'script/my_script.rb'
My Script is running!
irb(main):002:0>

Use the __END__ keyword to stop the parser. 使用__END__关键字来停止解析器。

puts "My Script is running!"
__END__
puts "This will never happen"
...

Or just comment out the rest of the script (if you need the a well-formed DATA stream after the ___END__ ) 或者只是注释掉脚本的其余部分(如果在___END__之后需要格式良好的DATA流)

puts "My Script is running!"
=begin
puts "This will never happen"
...
=end

If you need to exit early based on logic rather than at a fixed point, you could put the body of the script in a function and just return early. 如果你需要根据逻辑而不是在固定点提前退出,你可以将脚本的主体放在一个函数中,然后尽早返回。

def do_work
  puts "My Script is running!"
  ...
  return if exit_condition?
  puts "This will never happen if exit condition is true"
  ...
end

do_work

Alternatively, put your script in a begin block and rescue an exception you raise 或者,将您的脚本放在开始块中并挽救您引发的异常

begin
  puts "My Script is running!"
  raise SystemExit if exit_condition?
  puts "This will never happen if exit condition is true"
rescue SystemExit
end

The best solution I can think of right now is to raise an exception that isn't handled by the script. 我现在能想到的最好的解决方案是引发一个脚本无法处理的异常。 However, you get the stack trace output which is probably undesirable. 但是,您获得的堆栈跟踪输出可能是不合需要的。

script/my_script.rb 脚本/ my_script.rb

puts "My Script is running!"
raise "quiting, but I'm not happy about it"
puts "This will never happen"

Results: 结果:

irb(main):001:0> load 'script/my_script.rb'
My Script is running!
RuntimeError: quiting, but I'm not happy about it
    from script/my_script.rb:2:in `<top (required)>'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `block in load'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:245:in `load'
    from (irb):1
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /usr/local/rbenv/versions/1.9.3-p374/lib/ruby/gems/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
irb(main):002:0>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM