How do I start IRB console from a rake task?
Asked Answered
O

5

12

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
Okapi answered 21/4, 2010 at 14:33 Comment(0)
M
32

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
Melchor answered 20/5, 2010 at 18:18 Comment(3)
That was indeed the problem! Thanks for answeringOkapi
Thanks too! @MichaelLang you should accept this answer already.Arcuation
There is a more straightforward way to do this in Ruby 2.4.0; see my answer.Agle
A
3

As of Ruby 2.4.0, you can do this:

require 'irb'
binding.irb
Agle answered 12/1, 2017 at 3:10 Comment(1)
nice to know! I should probably note the original question and accepted answer solves for Ruby 1.8.Okapi
F
1

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
Fresh answered 1/5, 2010 at 12:55 Comment(0)
W
1

The rake file contents are below and it is named Rakefile.
Run it from terminal with rake test:console

require 'rubygems'
require 'rake'

namespace :test do
desc "Test Task"

 desc "Load stuff in IRB."
 task :console do

   exec "irb -r rubygems -r sanitize" #require multiple gems by typing -r gemname

 end

 end

once you've executed the rake test:console, irb pops up and you can see that it works by using Sanitize's clean method.
Sanitize.clean "some text"

Willms answered 3/2, 2011 at 20:50 Comment(0)
E
0

Apparently there must be a problem with how you defined your task. What happens if you change

task :foo => [:environment] do

to

task :foo => :environment do
Eleonoreleonora answered 21/4, 2010 at 14:38 Comment(1)
Same thing happens whether I have [:environment], just :environment, or nothing at all. If I change the name of the task to "bar" then IRB fusses about "bar" not being found, so its somehow picking up on the task name and trying to find a file or folder for it.Okapi

© 2022 - 2024 — McMap. All rights reserved.