lyo*_*eta 1 ruby string variables
我写了一些代码来获取用户的输入,然后根据我的需要改变它.我需要它以改变和未改变的形式,所以我将输入保存为两个变量.我不明白的是为什么两个变量都在变化.我尝试了一些额外的放置线来确定原因是什么,但我无法弄清楚.代码:
puts "Enter the full directory path of the flv files."
folder = gets.chomp
puts "Folder 1: " + folder
path = folder
path.slice!(0..6)
path.gsub!('\\', '/')
path += '/'
puts "Folder: " + folder
puts "Path: " + path
Run Code Online (Sandbox Code Playgroud)
使用输入:f:\ folder\subfolder\another
输出:
Folder 1: f:\folder\subfolder\another
Folder: folder/subfolder/another
Path: folder/subfolder/another/
Run Code Online (Sandbox Code Playgroud)
我想要的是获取一个目录并保留其他进程的目录,还要将其转换为URL友好格式.想法?
path = folder # does not actually copy the object, copies the reference
path.object_id == folder.object_id # the objects are the same, see
path.slice!(0..6) # all bang methods work with the same object
Run Code Online (Sandbox Code Playgroud)
因此,您path是对同一对象的引用folder.
要解决此问题,请使用
path = folder.clone
Run Code Online (Sandbox Code Playgroud)