我可以有条件地跳过需要 Ruby 中的文件吗?
begin
require 'aws-sdk'
rescue LoadError
puts "aws-sdk gem not found"
end
namespace :db do
desc "import local postgres database to heroku. user and database name is hardcoded"
task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
# code using aws-sdk gem
end
end
在上面的代码中,我可以让Ruby之后不要读取吗?rescue LoadError
我可以将整个代码包装在条件中,但这很不优雅。
我试过next
and return
.
编辑:添加了一个新问题我可以有条件地跳过在同一文件中加载“更多”Ruby 代码吗? https://stackoverflow.com/questions/14526268/can-i-conditionally-skip-loading-further-ruby-code-in-the-same-file。对不起。没有正确问这个问题
也许添加一个exit
日志之后:
begin
require 'aws-sdk'
rescue LoadError
puts "aws-sdk gem not found"
exit
end
namespace :db do
desc "import local postgres database to heroku. user and database name is hardcoded"
task :import_to_heroku => [:environment, "db:dump_for_heroku"] do
# code using aws-sdk gem
end
end
此外,中止功能是在同一调用中记录和退出:
abort("aws-sdk gem not found")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)