How do I start IRB console from a rake task?

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

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

从Ruby 2.4.0开始,你可以这样做:

require 'irb'
binding.irb

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
链接地址: http://www.djcxy.com/p/62874.html

上一篇: 我如何从头开始我的Ruby环境?

下一篇: 如何从rake任务启动IRB控制台?