基本上我想创建一个程序,可以删除3个目录和一个文件,以准备软件"重建"为了排除故障,需要经常这样做。
问题是,它需要在具有不同用户的多台计算机上使用,最初我只有一个批处理程序,我必须编辑相应的用户名。我试图自动化这个。
如何制作以便为用户名输入的内容放入FileUtils.rm_rf path?
我尝试${Username}
#{Username}
和@{Username}
,其中没有一个似乎有用吗?
另外,如果找不到特定文件,它将返回自定义消息而不是ruby错误,我怎么能这样做呢?
提前致谢!对于我的noobness抱歉,这是我的第一篇文章,我对ruby非常陌生(我甚至用Google搜索了如何使用' goto' in ruby!)
require 'fileutils'
puts "Please enter in your username:"
Username = gets.chomp
def question?
puts "Are you sure you want to continue? (Y/N)"
case gets.strip
when 'Y', 'y', 'yes' 'Yes'
FileUtils.rm_rf('C:\Users\USERNAME')
FileUtils.rm_rf('C:\Users\USERNAME')
FileUtils.rm_rf('C:\Users\USERNAME')
File.delete('C:\Users\USERNAME\file.txt')
puts "Files Successfully deleted"
sleep(5)
exit
when 'N', 'n', 'No', 'no'
exit
else
puts "Invalid Character!"
question?
end
end
question?
答案 0 :(得分:1)
改变这个:
require 'fileutils'
puts "Please enter in your username:"
Username = gets.chomp
def question?
puts "Are you sure you want to continue? (Y/N)"
case gets.strip
when 'Y', 'y', 'yes' 'Yes'
FileUtils.rm_rf('C:\Users\USERNAME')
FileUtils.rm_rf('C:\Users\USERNAME')
FileUtils.rm_rf('C:\Users\USERNAME')
File.delete('C:\Users\USERNAME\file.txt')
puts "Files Successfully deleted"
sleep(5)
exit
when 'N', 'n', 'No', 'no'
exit
else
puts "Invalid Character!"
question?
end
end
为:
require 'fileutils'
def question?
puts "Please enter in your username:"
username = gets.chomp
puts "Are you sure you want to continue? (Y/N)"
case gets.strip
when 'Y', 'y', 'yes' 'Yes'
FileUtils.rm_rf("C:/Users/#{username}")
File.delete("C:/Users/#{username}/file.txt") #I doubt you need this line
puts "Files Successfully deleted"
sleep(5)
exit
when 'N', 'n', 'No', 'no'
exit
else
puts "Invalid Character!"
question?
end
end
您只能在double_quoted字符串中插入变量,您希望将变量username
放在方法范围question?