gpt4 book ai didi

ruby - 变量变化

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:07 24 4
gpt4 key购买 nike

我编写了一些代码来获取用户的输入,然后根据我的需要对其进行修改。我需要改变和未改变的形式,所以我将输入保存到两个变量中。我不明白的是为什么这两个变量都在变化。我尝试了一些额外的 puts 行来确定原因是什么,但我无法弄清楚。代码:

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 友好格式。想法?

最佳答案

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

因此您的 path 是对与 folder 相同对象的引用。

要解决这个问题,请使用

path = folder.clone

关于ruby - 变量变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7890507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com