Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Current folder name in Ruby?

Tags:

ruby

Is there a simple way to get the name of the current folder name or do I have to do it with regexp?

like image 377
never_had_a_name Avatar asked Aug 27 '10 12:08

never_had_a_name


People also ask

How to check if a directory exists in Ruby?

If it matters whether the file you're looking for is a directory and not just a file, you could use File. directory? or Dir. exist? . This will return true only if the file exists and is a directory.

What is directory in Ruby?

A directory is a location where files can be stored. For Ruby, the Dir class and the FileUtils module manages directories and the File class handles the files.

How do you find the file path in Ruby?

Use the Ruby File. dirname method. For me, File. dirname("/a/b/c/d") correctly returns /a/b/c but File.

What is __ file __ in Ruby?

In Ruby, the Windows version anyways, I just checked and __FILE__ does not contain the full path to the file. Instead it contains the path to the file relative to where it's being executed from.


1 Answers

dirname = File.basename(Dir.getwd)

File.basename() returns the base name even when its argument is the path of a directory.

The following code prints kiamlaluno for me, where /home/kiamlaluno is the home directory in my Ubuntu installation.

puts File.basename('/home/kiamlaluno')

The same does puts File.basename(Dir.getwd), when the current directory is /home/kiamlaluno.

like image 91
apaderno Avatar answered Sep 21 '22 21:09

apaderno