Is there a simple way to get the name of the current folder name or do I have to do it with regexp?
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.
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.
Use the Ruby File. dirname method. For me, File. dirname("/a/b/c/d") correctly returns /a/b/c but File.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With