变量变化
我编写了一些代码来获取用户的输入,然后根据我的需要进行更改。我需要它的改变和未改变的形式,所以我将输入保存到两个变量中。我不明白的是为什么这两个变量都在变化。我尝试了一些额外的 put 行来确定原因是什么,但我无法弄清楚。代码:
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
输入:f:\folder\subfolder\another
输出:
Folder 1: f:\folder\subfolder\another
Folder: folder/subfolder/another
Path: folder/subfolder/another/
我想要获取一个目录并为其他进程保留该目录,同时将其转换为 URL 友好的格式。有想法吗?
I wrote some code to get input from a user and then alter it to my needs. I need it in the altered and unaltered form so I am saving the input into two variables. What I don't understand is why it both variables are changing. I tried some extra puts lines to determine what the cause is but I am unable to figure it out. The code:
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
With input: f:\folder\subfolder\another
Output:
Folder 1: f:\folder\subfolder\another
Folder: folder/subfolder/another
Path: folder/subfolder/another/
What I'm going for is getting a directory and keeping the directory for other processes, but also transforming it into a URL friendly format. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此,您的
路径
是对与文件夹
相同的对象的引用。要解决此问题,请使用
Thus your
path
is a reference to the same object asfolder
.To fix this, use
当您执行
b = a
时,它会使b
指向与a
相同的值,因此当您更改a
时使用诸如slice!
之类的 > 的值,b
也将指向更改后的值。为了避免这种情况,请复制该对象:
When you do
b = a
, it's makingb
point at the same value asa
, so when you changea
's value using something likeslice!
,b
will also point to the changed value.To avoid this, duplicate the object instead: