我已经看到两种常用的技术,用于将当前正在执行的文件的目录添加到$ LOAD_PATH(或$ :).如果你不使用宝石,我会看到这样做的好处.显然,一个看起来比另一个看起来更冗长,但有理由与其中一个相提并论吗?
第一个,冗长的方法(可能是过度杀伤):
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
Run Code Online (Sandbox Code Playgroud)
而且更直接,快速和肮脏:
$:.unshift File.dirname(__FILE__)
Run Code Online (Sandbox Code Playgroud)
有理由和其他人一起去吗?
当我通过采用Ruby语言进行旅程时,我花了很多时间在IRb中.真是太棒了!但是,由于我不太了解它的功能,并且仍然是Ruby的"小块",我想知道以下内容:
有谁知道如何从终端运行需要N个文件/宝石的ruby文件,并以IRB会话结束这些文件已加载到内存的操作?
换句话说,我希望是这样的:
$ ruby project_console.rb
# project_console.rb
IRB.new do |config|
require 'bundler/setup'
require 'import_project_file'
require_relative "spec/muffin_blog/app/models/random_file"
Post.establish_connection({database: "spec/muffin_blog/db/development.sqlite3"})
end
# yay. I'm in my custom IRB session with all of the above already loaded
2.4.1 :001 >
Run Code Online (Sandbox Code Playgroud)
与
$ irb
2.4.1 :001 > require 'bundler/setup'
=> true
2.4.1 :002 > require 'import_project_file'
=> true
2.4.1 :003 > require_relative "spec/muffin_blog/app/models/random_file"
=> true
2.4.1 :004 > Post.establish_connection({database: "spec/muffin_blog/db/development.sqlite3"})
# this makes me sad because its manual every time I want to play …Run Code Online (Sandbox Code Playgroud)