简体   繁体   中英

How to create a directory inside a directory with ruby and ARGV

I have a program that asks with the termial to write the name of the directory that the user wants, like this $ ruby app.rb mysuperdirectory

def get_ directory_name
  return directory_name = ARGV.first
end

def create_ directory(name)
  Dir.mkdir(name)
end

def perform
  directory_name = get_ directory_name
  create_ directory(directory_name)
end

perform

I will automatically want to create a lib directory Dir.mkdir("lib") and put it in the directory mysuperdirectory . In this mysuperdirectory that the user has just created with lib inside, I would like to make a system("git init") and system("rspec --init")

How can I do all this? Thanks

You can use Dir.chdir to change directory.

def perform
  directory_name = get_ directory_name
  create_ directory(directory_name)
  create_ directory("#{directory_name}/lib")
  Dir.chdir(directory_name) do
    system("git init")
    system("rspec --init")
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM