Raymond Raymond - 6 months ago 16
Ruby Question

Open directory and create new file

I have this statement that creates nested directories:

require 'fileutils'
FileUtils::mkdir_p ''+project_name+'/new_folder'


I want to add for example a new file called
README.md
inside
new_folder
. Is there a way to accomplish that with Ruby?

Answer

You just need to do

path_name = "#{project_name}/new_folder"
FileUtils::mkdir_p path_name
FileUtils::touch("#{path_name}/README.md")

This will create an empty file named README.md inside your project_name/new_folder directory.

Comments