Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I start IRB console from a rake task?

Tags:

ruby

rake

irb

I'm trying to write a rake task that will set up an environment mirroring my project.

task :environment do 
  require 'rubygems'
  require 'sequel'
  # require 'my_projects_special_files'
end

task :foo => [:environment] do
  require 'irb'
  IRB.start
end

Leads to irb complaining that "foo" doesn't exist (the name of the task)

10:28:01:irb_test >> rake foo --trace
(in /Users/mwlang/projects/personal/rake/irb_test)
** Invoke foo (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute foo
rake aborted!
No such file or directory - foo
/opt/local/lib/ruby/1.8/irb/input-method.rb:68:in `initialize'
/opt/local/lib/ruby/1.8/irb/input-method.rb:68:in `open'
/opt/local/lib/ruby/1.8/irb/input-method.rb:68:in `initialize'
/opt/local/lib/ruby/1.8/irb/context.rb:80:in `new'
/opt/local/lib/ruby/1.8/irb/context.rb:80:in `initialize'
/opt/local/lib/ruby/1.8/irb.rb:92:in `new'
/opt/local/lib/ruby/1.8/irb.rb:92:in `initialize'
/opt/local/lib/ruby/1.8/irb.rb:57:in `new'
/opt/local/lib/ruby/1.8/irb.rb:57:in `start'
/Users/mwlang/projects/personal/rake/irb_test/Rakefile:9
like image 374
Michael Lang Avatar asked Apr 21 '10 14:04

Michael Lang


People also ask

How do I run a rake in console?

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.

How do you call a rake task?

To run a rake task, just call the rake command with the name of your task. Don't forget to include your namespaces when you have them.

What is rake command?

Rake is a software task management and build automation tool created by Jim Weirich. It allows the user to specify tasks and describe dependencies as well as to group tasks in a namespace. It is similar in to SCons and Make.


2 Answers

IRB.start is looking at ARGV which contains the task name(s) from the rake command line. Try clearing ARGV first.

require 'irb'
ARGV.clear
IRB.start
like image 77
Heath Avatar answered Nov 10 '22 01:11

Heath


I've had a similar problem when running my task like that. Setting it the default task solved the problem but it did not help with the bug. Here: what i did

task :console do
  exec 'irb -I lib -r startingscript.rb'
end
like image 21
pastjean Avatar answered Nov 10 '22 01:11

pastjean