0
namespace:db do
    desc "Populates Database"
    task :populate => :environment do

    error = true

     if error
         puts "Error Code 1: There was an error!"
         return false
     end

end

ランニング

rake db:migrate

実際に期待される結果を引き起こします:

Error Code 1: There was an error!
rake aborted!
unexpected return

しかしその後:

/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:131:in `block (3 levels) in <top (required)>'
/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:104:in `each'
/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:104:in `block (2 levels) in <top (required)>'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:fertilize7
(See full trace by running task with --trace)
james@ubuntu:~/Documents/fertilizer/fertilizer$ rake db:fertilize7
rake aborted!
/home/james/Documents/fertilizer/fertilizer/lib/tasks/custom.rake:549: syntax error, unexpected end-of-input, expecting keyword_end
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `block in load'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in `load'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:641:in `block in run_tasks_blocks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:641:in `each'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:641:in `run_tasks_blocks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/application.rb:244:in `run_tasks_blocks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:446:in `load_tasks'
/home/james/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/james/Documents/fertilizer/fertilizer/Rakefile:6:in `<top (required)>'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/home/james/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>'
(See full trace by running task with --trace)
james@ubuntu:~/Documents/fertilizer/fertilizer$ 

予期しないリターンを報告して、そのエラー報告をすべて取り除くにはどうすればよいですか? これは、本当のエラー メッセージ (「エラー コード 1: エラーが発生しました!」) から気をそらします。

4

1 に答える 1

7

あなたはメソッドにいるのではなく、プロシージャにいます。戻ることはできませんnext。代わりに、proc から飛び出す必要があります。

error = true

 if error
     puts "Error Code 1: There was an error!"
     next
 end
于 2013-10-12T13:12:40.897 に答える