Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
communitycreator

What is File.dirname() in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

dirname() is a File class method in Ruby that returns all components of a specified file name, except the last one. For example, /home/okwudili returns /okwudili when invoked.

Syntax

File.dirname(file_name)

Parameters

  • file_name: This is the file name whose components we want to return.

Return value

This method returns a string containing all components of the file name file_name, except the last one.

Code example

# Get the directory names of the files
puts File.dirname("/home/okwudili/code/main.rb")
puts File.dirname("/main.rb")
puts File.dirname("/home/okwudili/code/ruby/main.rb")
Getting the dirname() of a file in Ruby

Explanation

  • Lines 2 to 4: We get the directory names of some files using the dirname() method. Then, we print the values to the console.

RELATED TAGS

ruby
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring