自动加载项目环境到irb

发布于 2024-10-25 14:36:02 字数 182 浏览 4 评论 0原文

Rails 有一个有用的命令 rails console,它会下载所有必要的数据,然后我们可以与 irb 中的 Rails 项目进行交互。 Ruby 项目(基于 Ruby 语言构建)是否有相同的技术?通过这个技巧,我可以在 irb 中使用 Ruby 项目,而无需担心加载库、模块、类、文件等。 谢谢

Rails has useful command rails console, which downloads all necessary data and then we can interact with rails project in irb. Is there the same technique for Ruby project (built on Ruby language)? By this trick I can play with Ruby project in the irb without concerning about loading libraries, modules, classes, files and so on.
Thanks

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

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

发布评论

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

评论(4

笑忘罢 2024-11-01 14:36:02

您的项目应该有一个加载环境的文件。假设您的项目位于 lib/project.rb 中,那么只需:

$ irb -Ilib -rproject

Your project should have one file which loads the environment. Assuming your project is in lib/project.rb then simply:

$ irb -Ilib -rproject
奈何桥上唱咆哮 2024-11-01 14:36:02

来自我的一个项目:

# Creates an IRB console useful for debugging experiments
# Loads up the environment for the condition passed
def console
  File.open("./tmp/irb-setup.rb", 'w') do |f|
    f.puts "# Initializes the environment for IRb."
    f.puts "Code to initialize your project here"
    f.puts "$: << '#{File.expand_path(".")}/'"  #handle load path       
  end
  irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
  # require your code
  libs =  " -r irb/completion"
  libs <<  " -r #{File.dirname(__FILE__) + "/base"}"
  libs << " -r ./tmp/irb-setup.rb" # require the config file you just wrote
  puts "Loading #{@options.env} environment..."
  exec "#{irb} #{libs} --simple-prompt"
end

诀窍是您构建 irb 命令来自动要求您需要的所有代码。我还需要设置一些配置,因此我添加了编写一个我需要在 IRb 中的文件的魔法。

From one of my projects:

# Creates an IRB console useful for debugging experiments
# Loads up the environment for the condition passed
def console
  File.open("./tmp/irb-setup.rb", 'w') do |f|
    f.puts "# Initializes the environment for IRb."
    f.puts "Code to initialize your project here"
    f.puts "$: << '#{File.expand_path(".")}/'"  #handle load path       
  end
  irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
  # require your code
  libs =  " -r irb/completion"
  libs <<  " -r #{File.dirname(__FILE__) + "/base"}"
  libs << " -r ./tmp/irb-setup.rb" # require the config file you just wrote
  puts "Loading #{@options.env} environment..."
  exec "#{irb} #{libs} --simple-prompt"
end

The trick is that you construct the irb command to autorequire all the code you need. I also needed to set up some configuration so I add the magick of writing a file I then require in IRb.

绝影如岚 2024-11-01 14:36:02

就我而言,我的初始化脚本位于当前工作目录中。下面的内容对我有用。

irb -r ./setup.rb

In my case my initialization script was in the current working directory. The below worked for me.

irb -r ./setup.rb
自演自醉 2024-11-01 14:36:02

对于任何项目,您还可以添加 .irbrc,并在其中需要您想要的内容,很可能是项目的入口点。

require "lib/project"

For any project, you could also add a .irbrc, and require what you want in there, most likely the entry point of the project.

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