Patch attempt for ActiveRecord::Base#clone

All,

I am attempting to patch ActiveRecord::Base#clone (what I think the bug
is can be another thread), and I am having difficulty navigating the
Ruby magic.

The last 3 lines in clone() are:

self.class.new do |record|
record.send :instance_variable_set, ‘@attributes’, attrs
end

I try to replace it with the code below - basically pulling the
assignment of the @attributes hash outside of the new instantiation.

newobj = self.class.new
newobj.instance_variable_set(’@attributes’, attrs)

and it breaks three tests.

  1. Why do these two pieces of code not produce the same results?

  2. In the original code, does the variable “record” represent a new
    instance of the class?

  3. Unfortunately, I can’t even tell if self.class.new is calling the
    class method “new” or the instance method “new”. Looks like the
    class method takes a block and the instance method doesn’t. It makes
    sense to me that my code is not working if the original code deals with
    the class method. But then I don’t really understand what that original
    code returns.

Any help would be appreciated.

Thanks,
Wes

OK, it’s late and I wasn’t thinking clearly.

This code will effectively do the same thing as the original code:

newobj = self.class.new
newobj.instance_variable_set(’@attributes’, attrs)
newobj

I was forgetting that the return valid of the do…end block in the
original code was what was being returned.

And, of course, it’s the instance method “new”. Duh.

What is interesting, however, is that I didn’t realize that you could
pass a block to new. Sort of like a quickie “initialize” method?

Sorry for the brain fart.

Wes

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs