LoginSignup
0
0

More than 5 years have passed since last update.

require可能なライブラリの一覧を取得

Posted at

プログラム内からrequire可能なライブラリの一覧を表示する を読んだのですが、UNIXのfindコマンドを使っているので、Windowsでも使えるようPure Rubyで書けないかと思ってやってみました。
ついでに一応.rbだけでなく.soも検索するようにしています。

puts $:.flat_map {|path|
  Dir.glob(File.expand_path("**/*.{rb,so}", path))
}

findを使うパターンと結果を比べてみます。findは-oオプションでOR検索できます。順番を揃えるため両方の結果をソートしています。

$ ruby -e 'puts $:.flat_map {|path| Dir.glob(File.expand_path("**/*.{rb,so}", path))}.sort' >ruby-search
$ find `ruby -e 'print $:*" "'` -type f -name "*.rb" -o -name "*.so" | sort >find-search
$ diff -s ruby-search find-search
ファイル ruby-search と find-search は同一です
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0