Forum: Ruby on Rails Patch attempt for ActiveRecord::Base#clone

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-01-18 08:27

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: do |record|
  record.send :instance_variable_set, '@attributes', attrs

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

newobj =
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 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.

Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2007-01-18 08:58
OK, it's late and I wasn't thinking clearly.

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

newobj =
newobj.instance_variable_set('@attributes', attrs)

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.

This topic is locked and can not be replied to.