如何从rake任务启动IRB控制台?

时间:2022-04-29 07:12:19

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

我正在尝试编写一个rake任务,该任务将设置一个镜像我的项目的环境。

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)

导致irb抱怨“foo”不存在(任务的名称)

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

5 个解决方案

#1


30  

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

IRB.start正在查看ARGV,其中包含rake命令行中的任务名称。首先尝试清除ARGV。

require 'irb'
ARGV.clear
IRB.start

#2


2  

As of Ruby 2.4.0, you can do this:

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

require 'irb'
binding.irb

#3


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

#4


0  

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

#5


0  

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

rake文件内容如下,名为Rakefile。使用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"

一旦你执行了rake测试:console,irb弹出,你可以看到它使用Sanitize的干净方法。 Sanitize.clean“some text”

#1


30  

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

IRB.start正在查看ARGV,其中包含rake命令行中的任务名称。首先尝试清除ARGV。

require 'irb'
ARGV.clear
IRB.start

#2


2  

As of Ruby 2.4.0, you can do this:

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

require 'irb'
binding.irb

#3


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

#4


0  

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

#5


0  

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

rake文件内容如下,名为Rakefile。使用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"

一旦你执行了rake测试:console,irb弹出,你可以看到它使用Sanitize的干净方法。 Sanitize.clean“some text”