Looking for the best way to exit a loaded script in Ruby on Rails's IRB. Kernel.exit & Kernel.abort will pop you out of the IRB session altogether. Ideally, this would happen on some conditional criteria.
My script/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.
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__
)
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
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.