Skip to content

Instantly share code, notes, and snippets.

@shuuuuun
Last active March 21, 2021 01:48
Show Gist options
  • Save shuuuuun/54a840836f54d7668c37b03fa7c965b3 to your computer and use it in GitHub Desktop.
Save shuuuuun/54a840836f54d7668c37b03fa7c965b3 to your computer and use it in GitHub Desktop.
rubyのFileまわりで忘れがちなやつメモ
#!/usr/bin/env ruby
puts "__FILE__: #{__FILE__}"
puts "__dir__: #{__dir__}"
puts "File.dirname(__FILE__): #{File.dirname(__FILE__)}"
puts "File.expand_path('.', __FILE__): #{File.expand_path('.', __FILE__)}"
puts "File.expand_path('.', __dir__): #{File.expand_path('.', __dir__)}"
# スクリプトが1階層下にある場合
APP_ROOT = File.expand_path('..', __dir__)
puts "APP_ROOT: #{APP_ROOT}"
# ex.
# $ cd /path/to/project
# $ scripts/hoge.rb
# __FILE__: scripts/hoge.rb
# __dir__: /path/to/project/scripts
# File.dirname(__FILE__): scripts
# File.expand_path('.', __FILE__): /path/to/project/scripts/hoge.rb
# File.expand_path('.', __dir__): /path/to/project/scripts
# APP_ROOT: /path/to/project
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment