这是 ruby 错误吗?
target_url_to_edit = target_url
if target_url_to_edit.include?("http://")
target_url_to_edit["http://"] = ""
end
logger.debug "target url is now #{target_url}"
这会返回没有 http://的 target_url
最佳答案
您需要复制内存中的对象,因为变量名只是对内存中对象的引用:
target_url_to_edit = target_url.dup
现在 target_url_to_edit
被分配了原始对象的新副本。
对于您的情况,此代码可能只在一行中执行相同的操作(没有重复,没有 if):
target_url_to_edit = target_url.sub(%r{^http://}, "")
关于Ruby 怪异的赋值行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3029263/