たくろぐ!

仕事はエンジニア、心はアーティスト

requireのload_pathについて調べてみた

require './lib/rgb'

なぜカレントディレクトリをさすのに'./'なのか。

Ruby1.8系くらいまではデフォルトでカレントディレクトリをrequireできたらしい。

今は$LOAD_PATH以外のディレクトリ以外を指定するときはrequireメソッドの引数にパスをつけてあげる必要があるらしい。

なぜカレントディレクトリは'./'なのか(2回目)。

rubygemsのようなライブラリは$LOAD_PATHに含まれている

早速$LOAD_PATH変数を出力してみた。
Ubuntuecho $LOAD_PATHしたら何も表示されなかった。
なぜ?

vagrant@ubuntu-xenial:~/project/cherry_ruby/ruby-book$ mkdir tmp
vagrant@ubuntu-xenial:~/project/cherry_ruby/ruby-book$ touch tmp/require_path.rb
vagrant@ubuntu-xenial:~/project/cherry_ruby/ruby-book$ vi tmp/require_path.rb 
vagrant@ubuntu-xenial:~/project/cherry_ruby/ruby-book$ cat tmp/require_path.rb 
p $LOAD_PATH
p $: # $LOAD_PATHと同じ意味
vagrant@ubuntu-xenial:~/project/cherry_ruby/ruby-book$ ruby tmp/require_path.rb 
["/home/vagrant/.rbenv/rbenv.d/exec/gem-rehash", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/x86_64-linux", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/site_ruby", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0/x86_64-linux", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/x86_64-linux"]
["/home/vagrant/.rbenv/rbenv.d/exec/gem-rehash", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/site_ruby/2.5.0/x86_64-linux", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/site_ruby", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby/2.5.0/x86_64-linux", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/vendor_ruby", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0", "/home/vagrant/.rbenv/versions/2.5.0/lib/ruby/2.5.0/x86_64-linux"]


vagrant@ubuntu-xenial:~/project/cherry_ruby/ruby-book$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 2.7.7
  - RUBY VERSION: 2.5.0 (2017-12-25 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0
  - USER INSTALLATION DIRECTORY: /home/vagrant/.gem/ruby/2.5.0
  - RUBY EXECUTABLE: /home/vagrant/.rbenv/versions/2.5.0/bin/ruby
  - EXECUTABLE DIRECTORY: /home/vagrant/.rbenv/versions/2.5.0/bin
  - SPEC CACHE DIRECTORY: /home/vagrant/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /home/vagrant/.rbenv/versions/2.5.0/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/vagrant/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0
     - /home/vagrant/.gem/ruby/2.5.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
     - "install" => "--no-ri --no-rdoc"
     - "update" => "--no-ri --no-rdoc"
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/vagrant/.rbenv/versions/2.5.0/bin
     - /home/vagrant/.rbenv/libexec
     - /home/vagrant/.rbenv/plugins/ruby-build/bin
     - /home/vagrant/bin
     - /home/vagrant/.local/bin
     - /home/vagrant/.pyenv/shims
     - /home/vagrant/.pyenv/bin
     - /home/vagrant/.anyenv/envs/pyenv/shims
     - /home/vagrant/.anyenv/envs/pyenv/bin
     - /home/vagrant/.anyenv/bin
     - /home/vagrant/.rbenv/shims
     - /home/vagrant/.rbenv/bin
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /usr/local/games
     - /snap/bin

'./'のほかの書き方

require './'でカレントディレクトリをロードすることができる。
$:.unshift './lib'でもいいらしい。
実際にちゃんと読み込んでくれていた。

うーん、謎。