Ruby: File Path Basics

/ Published in: Ruby
Save to your folder(s)

Ruby provides a platform independent way of displaying file paths. Here are a few common ways to show absolute and relative paths.

Copy this code and paste it in your HTML
  1. #Platform independant way of showing a File path. Empty String ('') means the root
  2. puts File.join('', 'Users', 'chrisaiv', 'Desktop')
  4. #Provide the RELATIVE path of THIS file
  5. puts __FILE__
  7. #Provide an ABSOLUTE path
  8. puts File.expand_path(__FILE__)
  10. #Provide the RELATIVE directory where this path is located
  11. puts File.dirname(__FILE__)
  13. #"Moves Up" one directory and enter another folder
  14. puts File.join(File.dirname(__FILE__), '..', 'NML' )
  17. #Tell Ruby all the folders as to where to look for us. In this case, look within the /lib
  18. APP_ROOT = File.dirname(__FILE__)
  19. $:.unshift( File.join( APP_ROOT, 'lib') )

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.