Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asking questions in rake tasks

Tags:

ruby

rake

I have a rake task that is called from another rake task.

In this rake task I need to ask the user for some text input, and then, depending on the answer either continue, or stop everything from continuing (including the calling rake task).

How can I do this?

like image 826
Neil Middleton Avatar asked Mar 07 '11 14:03

Neil Middleton


People also ask

How do I test Rake tasks?

Whenever you are testing Rake tasks, you need to load the tasks from the Rails application itself. Note that in your tests you should change MyApplication to the name of your application. This line locates the task by it's name and returns a Rake::Task object. Then, we call invoke on it, which executes the task.

How do you fail a Rake task?

You can use abort("message") to gracefully fail rake task. It will print message to stdout and exit with code 1.

What are Rake tasks for?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

How do you call a Rake task?

Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.


1 Answers

task :input_test do   input = ''   STDOUT.puts "What is the airspeed velocity of a swallow?"   input = STDIN.gets.chomp   raise "bah, humbug!" unless input == "an african or european swallow?" end task :blah_blah => :input_test do  end 

i think that should work

like image 72
loosecannon Avatar answered Oct 13 '22 14:10

loosecannon