[rspec-users] [RSpec] Cloning objects and leaking state

Nick Hoffman nick at deadorange.com
Tue Jan 27 22:52:09 EST 2009

On 27/01/2009, at 7:44 PM, David Chelimsky wrote:
> On Tue, Jan 27, 2009 at 1:48 PM, Nick Hoffman <nick at deadorange.com>  
> wrote:
>> G'day folks. I've been beating my head on this one problem for a  
>> couple of
>> hours, and have managed to figure out what's causing it. However, I  
>> don't
>> understand why it's happening, nor do I know how to solve or get  
>> around it.
>> One of my methods clones an arg, and it seems that doing so causes  
>> state to
>> leak out. I discovered this because changing this:
>> new_subtitle = subtitle.clone
>> to this:
>> new_subtitle = subtitle
>> causes the problem to disappear.
>> Before we get into the code snippets, are there any known caveats,  
>> warnings,
>> or problems with writing specs that cover the cloning or  
>> duplication of
>> objects?
>> Here's the code, specs, and spec output:
>> http://gist.github.com/53482
>> As you can see, in the "should clone ..." example, the expectation  
>> on line
>> 24 succeeds, but the same expectation on line 28 fails. After that,  
>> the same
>> expectation fails in the "should not leak state" example.
> Would you please try doing the same thing without using the clone
> method specifically. i.e. change the method name in the spec to :copy
> and then call that in the subject code as well and see if you come up
> w/ different results. I want to make sure this is really about the
> clone method itself.

I removed the call to #clone from the instance method, and lo and  
behold, the problem persists:
I don't know why, because when I posted the first email in this  
thread, this seemed to resolve it.

I also tried replacing #clone with #dup, and the problem still occurs:

Now I'm even more flummoxed. If the #clone isn't causing this problem,  
what is?

Can I do anything else to help others help me with this?

Thanks, guys!

More information about the rspec-users mailing list