File.expand_path

I have a simple question about the method expand_path. If we call File.expand_path like so:

File.expand_path('../btree', __FILE__)

What’s the accepted format for the string filename - ‘…/btree’? All the examples I’ve read seem to support a Bash command line format.

File.expand_path(file_name [, dir_string] Converts a pathname to an absolute pathname - according to rdocs.

When you write File.expand_path('../btree', __FILE__) it will return "#{Dir.pwd}/btree". For example, if you are in the /tmp directory, File.expand_path('../btree', __FILE__) returns “/tmp/btree”.

Let’s take some other examples:

File.expand_path('btree', 'f')
=> "/tmp/f/btree"

File.expand_path('btree', __FILE__)    # If you are in IRB
=> "/tmp/(irb)/btree"

File.expand_path(__FILE__)
=> "/tmp/(irb)"

File.expand_path('../btree', __FILE__)
=> "/tmp/btree"

The last line returns /tmp/btree. At first you need to consider the second argument. It takes you to the /tmp/(irb). Then the first argument takes you to the parent directory of (irb), which is /tmp/ again. This way expand_path ended up returning the /tmp/btree/.

Coming to the second part of your question, …/btree is a valid directory.

File.expand_path('.../btree')
=> "/tmp/.../btree"

I don’t know about Windows or Mac, but on GNU/Linux (especially I am using the XFS file system and my ramdisk) you can create a directory called ‘…’, ‘?’, ‘<’, ‘|’ etc (except unescaped /). Yes, really!

Sorry but there was no second part to the question. When I posted the …/btree the editor added the extra period.

I just want to know if the example I posted was portable.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs