简体   繁体   中英

Create directory with ARGV (ruby)

I created this program to create a folder in the program directory if there is an ARGV.

def check_if_user_gave_input
  abort("mkdir: missing input") if ARGV.empty?
end

def get_folder_name
  return folder_name = ARGV.first
end

def create_folder(name)
  Dir.mkdir(name)
end

def perform
  folder_name = get_folder_name
  create_folder(folder_name)
end

perform

So, if I run this program in my terminal everything is OK. But, if I try it in my terminal and don't write anything after $ ruby app.rb I get a nice error message like this and I don't see the string "mkdir: missing input"

Traceback (most recent call last):
    3: from app.rb:18:in `<main>'
    2: from app.rb:15:in `perform'
    1: from app.rb:10:in `create_folder'
app.rb:10:in `mkdir': no implicit conversion of nil into String (TypeError)

How to fix this? Thank you.

Just add check_if_user_gave_input method in perform

def check_if_user_gave_input
  abort("mkdir: missing input") if ARGV.empty?
end

def get_folder_name
  ARGV.first
end

def create_folder(name)
  Dir.mkdir(name)
end

def perform
  check_if_user_gave_input
  folder_name = get_folder_name
  create_folder(folder_name)
end

perform

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