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

发布于 2024-08-29 08:38:27 字数 1054 浏览 10 评论 0原文

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

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

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

导致 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

飘然心甜 2024-09-05 08:38:27

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

require 'irb'
ARGV.clear
IRB.start

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
把昨日还给我 2024-09-05 08:38:27

从 Ruby 2.4.0 开始,您可以执行以下操作:

require 'irb'
binding.irb

As of Ruby 2.4.0, you can do this:

require 'irb'
binding.irb
烟若柳尘 2024-09-05 08:38:27

我在运行我的任务时遇到了类似的问题。将其设置为默认任务解决了问题,但对解决错误没有帮助。在这里:我做了什么

task :console do
  exec 'irb -I lib -r startingscript.rb'
end

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
新一帅帅 2024-09-05 08:38:27

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

后,使用 rake test:console 从终端运行它,会弹出 irb,您可以看到它通过使用 Sanitize 的 clean 方法工作。
Sanitize.clean“一些文本”

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"

谁的新欢旧爱 2024-09-05 08:38:27

显然你定义任务的方式肯定有问题。如果您更改

task :foo => [:environment] do

为会发生什么

task :foo => :environment do

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文