Skip to content

Welcome to Tech by Example

Menu
  • Home
  • Posts
  • System Design Questions
Menu

Check if a file is writable or not in Ruby language

Posted on August 4, 2021August 4, 2021 by admin

Overview

File class of ruby provides the below method that can be used to check if a file is writable by the effective user and group id of the current process.

Here is the signature of the function

writable?(file_name)

It returns true if the file is writable or else it will return false.

Program

Create a file named test.txt in the current directory. With the same user run the below program

writable = File.writable?("test.txt")
puts writable

Output

true

Now change the user and group id of the test.txt file using chown command. Make sure that the current user doesn’t belong to the new group id.

Run the above program again. This time it will output false.

©2025 Welcome to Tech by Example | Design: Newspaperly WordPress Theme