简体   繁体   English

在irb控制台中需要一个lib目录

[英]Require a lib directory in irb console

Is there a way how to require all files in lib directory at once in irb console?: 有没有办法如何在irb控制台中同时require lib目录中的所有文件?:

irb ( project root )
require './lib/' # not working  

structure 结构体

.
|
--lib 
  |
  |-- one.rb
  |-- two.rb
  |-- tree.rb

EDIT 编辑

I prefer a solution where I can require files only once, not each time when I start irb session. 我更喜欢一种解决方案,我只require一次文件,而不是每次启动irb会话。

As described in the documentation require (and require_relative ) can't take the name of a directory as argument, just a single file name. 文档中所述, require (和require_relative )不能将目录名称作为参数,只需一个文件名。 You could write something like the following to do what you want: 您可以编写类似以下内容的内容来执行您想要的操作:

Dir['./lib/*.rb'].each { |f| require_relative(f) }

Create a file named .irbrc in your home directory, and write require commands for whatever file you want to require in there. 在您的主目录中创建一个名为.irbrc的文件,并在那里为您require的任何文件编写require命令。 When you run irb , .irbrc will be loaded. 运行irb ,将加载.irbrc

你可以用它

Dir[File.dirname(__FILE__) + '/lib/*.rb'].each {|file| require file }

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

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