Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesome/839b6b9b645d2ff74ff75e9db225a2c9 to your computer and use it in GitHub Desktop.
Save awesome/839b6b9b645d2ff74ff75e9db225a2c9 to your computer and use it in GitHub Desktop.
How to get the name of the current file with directory filepath using ruby. @awesome #soawesomeman
# $ ruby -v
# ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18]
# $ pwd
# /Users/dev/Documents/Github/Gists/awesome/
# $ echo "puts File.expand_path(__FILE__)" > how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
# $ echo "puts File.absolute_path(__FILE__)" >> how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
# $ cat how-to-get-the-name-of-the-current-file-with-directory-filepath.rb
# puts File.expand_path(__FILE__)
# puts File.absolute_path(__FILE__)
# $ ruby how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
# /Users/dev/Documents/Github/Gists/awesome/how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
# /Users/dev/Documents/Github/Gists/awesome/how-to-get-the-name-of-the-current-file-with-directory-filepath-using-ruby.rb
puts File.expand_path(__FILE__)
puts File.absolute_path(__FILE__)
# docs:
# https://ruby-doc.org/core-2.0.0/File.html#method-c-expand_path
# https://ruby-doc.org/core-2.0.0/File.html#method-c-absolute_path
# https://www.rubydoc.info/github/rubyworks/facets/Binding#__FILE__-instance_method
# http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-__FILE__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment