简体   繁体   English

我如何让 irb 需要运行 irb 的目录中的 config/boot.rb 文件?

[英]How would I have irb require the config/boot.rb file from the directory I run irb in?

In every directory that I run irb, I have a config/boot.rb file that needs to be required.在我运行 irb 的每个目录中,我都有一个需要的 config/boot.rb 文件。 It be great if this could be autoloaded when I enter irb, but I'm not sure how to refer to the directory that I opened irb in from the.irbc file.如果可以在我输入 irb 时自动加载它,那就太好了,但我不确定如何从 .irbc 文件中引用我打开 irb 的目录。

I would do:我会做:

 config_file = File.join(Dir.pwd, 'config', 'boot.rb')
 require config_file if File.exist?(config_file)

That should be your current directory:那应该是您的当前目录:

Dir.pwd

If you want, you could just require it like this:如果你愿意,你可以像这样要求它:

require File.expand_path('config/boot.rb', Dir.pwd)

Just use irb -r .只需使用irb -r

$ irb -r./config/boot

Bonus points if you bind it to a bash alias.如果将其绑定到 bash 别名,则会获得奖励积分。

$ alias bootirb=irb -r./config/boot

You won't be modifying your .irbrc this way—I'd imagine some people may find this to be a cleaner approach.您不会以这种方式修改您的.irbrc — 我想有些人可能会发现这是一种更清洁的方法。

You can get the current directory of the process with Dir.pwd and that should be the same as the directory where you run the irbc command.您可以使用Dir.pwd获取进程的当前目录,该目录应该与运行 irbc 命令的目录相同。 Try putting something like this in.irbc:尝试在.irbc 中加入这样的内容:

require Dir.pwd + '/config/boot.rb'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM