Get Parent Directory of Current Directory in Ruby

Get parent directory of current directory in Ruby

File.expand_path("..", Dir.pwd)

Ruby get parent dir of current file

You should use File.expand_path docs:

wb = Spreadsheet.open File.expand_path("../data/zipcode_range.xls", __FILE__)

Assuming, that the file you posted lies in the same directory as data/. If data/ is in the parent directory of your current Ruby file, go up twice:

wb = Spreadsheet.open File.expand_path("../../data/zipcode_range.xls", __FILE__)

The first ../ always neutralizes the __FILE__ when using File.expand_path this way. If you are not sure, make a debugging output of the file path that was generated:

puts File.expand_path("../data/zipcode_range.xls", __FILE__)

How to get the name of the folders from the file name in Ruby?

The Pathname class from the Ruby standard library can be used to break down paths into their constituent parts:

require 'pathname'
path = Pathname.new("E:/aaa/bbb/ccc/filename.jpg")
path.dirname.each_filename.drop(1) # ["aaa", "bbb", "ccc"]

How do I get the path without the last folder using ruby?

File.expand_path("..", Dir.pwd)

Create new directory in parent directory of current directory -- ROR FileUtils.mkdir

To define that directory as a Pathname:

path = Rails.root.join('..', 'new')

To create it on disk:

path.mkpath

To check if it already exists:

path.exist?

See the Pathname documentation for more things you can do.

Load File from parent directory

This is what I've done instead:

File.expand_path("../../neededsript.rb",File.dirname(__FILE__))

Change Directory with Ruby (with side effects?)

No, it is not possible.

In fact, no child process can change the current working directory of its parent process.

When you execute a script (or any program) from your command shell you are actually doing a "fork/exec" pair, which means you create a "child process" which is separate from your shell "parent process" in many ways. The child can make changes to its own environment but cannot (typically) change the parent environment.

How to navigate two directories up ruby

There are two ways to do this (well, there are several, but here are two good ones). First, using File.expand_path:

original_path = "/repos/something/test-folder/features/support"
target_path = File.expand_path("../../lib/resources", original_path)

p target_path
# => "/repos/something/test-folder/lib/resources"

Note that File.expand_path will always return an absolute path, so it's not appropriate if you want to get a relative path back. If you use it, either make sure that the second argument is an absolute path, or, if it's a relative path, that you know what it will expand to (which will depend on the current working directory).

Next, using Pathname#join (which is aliased as Pathname#+):

require "pathname"

original_path = Pathname("/repos/something/test-folder/features/support")
target_path = original_path + "../../lib/resources"

p target_path
# => #<Pathname:/repos/something/test-folder/lib/resources>

You can also use Pathname#parent, but I think it's kind of ugly:

p original_path.parent.parent
# => #<Pathname:/repos/something/test-folder>

p original_path.parent.parent + "lib/resources"
# => #<Pathname:/repos/something/test-folder/lib/resources>

I prefer Pathname because it makes working with paths very easy. In general you can pass the Pathname object to any method that takes a path, but once in awhile a method will balk at anything other than a String, in which case you'll need to cast it as a string first:

p target_path.to_s
# => "/repos/something/test-folder/lib/resources"

P.S. foo = "#{bar}" is an antipattern in Ruby. If bar is already a String you should just use it directly, i.e. foo = bar. If bar isn't already a String (or you're not sure), you should cast it explicitly with to_s, i.e. foo = bar.to_s.

P.P.S. Global variables are a nasty code smell in Ruby. There is almost always a better way to go than using a global variable.

Getting a list of folders in a directory

Jordan is close, but Dir.entries doesn't return the full path that File.directory? expects. Try this:

 Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') }


Related Topics



Leave a reply



Submit