Ruby by Example / File IO

file = File.open "file-name"

# Get content of file

content = file.read

# Write the content to the file
file.write "#{content} writed into file"

# Commit the change
file.close

Rename

File.rename "old-name", "new-name"

Get the size in byte of the file

File.size "file-name"

Check if the file exist

File.exist? "file-name"

Get the extension name from the file name

File.extname "file-name.ext"

Get the file name without the directory part

File.basename "dir/file-name"

Get the path for this file, without the file name

File.dirname "dir/file-name"

Check if it’s directory

File.directory? "file-name"