【发布时间】:2019-03-18 04:08:52
【问题描述】:
我需要克隆一个现有对象并更改该克隆对象。 问题是我的更改改变了原始对象。 代码如下:
require "httparty"
class Http
attr_accessor :options
attr_accessor :rescue_response
include HTTParty
def initialize(options)
options[:path] = '/' if options[:path].nil? == true
options[:verify] = false
self.options = options
self.rescue_response = {
:code => 500
}
end
def get
self.class.get(self.options[:path], self.options)
end
def post
self.class.post(self.options[:path], self.options)
end
def put
self.class.put(self.options[:path], self.options)
end
def delete
self.class.put(self.options[:path], self.options)
end
end
场景:
test = Http.new({})
test2 = test
test2.options[:path] = "www"
p test2
p test
输出:
#<Http:0x00007fbc958c5bc8 @options={:path=>"www", :verify=>false}, @rescue_response={:code=>500}>
#<Http:0x00007fbc958c5bc8 @options={:path=>"www", :verify=>false}, @rescue_response={:code=>500}>
有没有办法解决这个问题?
【问题讨论】:
-
我能问一下你想用这个猴子补丁做什么吗?我正在尝试测试您的代码,但它们似乎不起作用。