# Dynamic methods for copying objects

**URL:** <https://www.ruby-forum.com/t/dynamic-methods-for-copying-objects/138759>\
**Category:** Rails\
**Created:** [May 27, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/dynamic-methods-for-copying-objects/138759 "2008-05-27T14:32:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 27, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/dynamic-methods-for-copying-objects/138759/1 "2008-05-27T14:32:52Z")

</div>

Hi,

I was wondering if this is possible.

dyn\_method = “roles”  
Object1 = User.find(1)  
object2 = User.find(2)

object1.dyn\_method = object2.dyn\_method

So what im going to have is a load of strings coming in which are  
related to the object and i want to copy everything from object2 to  
object1 in the case above. Im going to be doing this several times for  
different methods related so i would rather one method to this and i  
just tell it what method and pass the objects.

dyn\_method = “addresses”

object1.dyn\_method = object2.dyn\_method

Ive tried various things like:

dyn\_method = “roles”.to\_syn

object1.send(dyn\_method, object2.dyn\_method)

Anyone got any ideas?

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 27, 2008, 3:27pm UTC](https://www.ruby-forum.com/t/dynamic-methods-for-copying-objects/138759/2 "2008-05-27T15:27:40Z")

</div>

On 27 May 2008, at 13:32, John B. wrote:

> Hi,
> 
> I was wondering if this is possible.
> 
> dyn\_method = “addresses”
> 
> object1.dyn\_method = object2.dyn\_method

object2.send(dyn\_method) will get you back the expected value. The key  
fact here are

- that an accessor is a pair of methods: a reader and a writer
- a.b corresponds to calling the method b
- a.b = c correspons to calling the method b=, with parameter c.

Armed with this it should be obvious: object1.send(dyn\_method + ‘=’,  
object2.send(dyn\_method))

Fred

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 27, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/dynamic-methods-for-copying-objects/138759/3 "2008-05-27T15:56:50Z")

</div>

Frederick C. wrote:

> On 27 May 2008, at 13:32, John B. wrote:
> 
> > Hi,
> > 
> > I was wondering if this is possible.
> > 
> > dyn\_method = “addresses”
> > 
> > object1.dyn\_method = object2.dyn\_method
> 
> object2.send(dyn\_method) will get you back the expected value. The key  
> fact here are
> 
> - that an accessor is a pair of methods: a reader and a writer
> - a.b corresponds to calling the method b
> - a.b = c correspons to calling the method b=, with parameter c.
> 
> Armed with this it should be obvious: object1.send(dyn\_method + ‘=’,  
> object2.send(dyn\_method))
> 
> Fred

ok thanks, i see where your coming from now. If object1 already had 2  
roles would it be possible to add more objects. So if object1 had 2  
roles already i will have to check for different roles that object2 has  
and add any different ones. The above line of code overwrites the  
exisiting roles with the roles that object 2 has.

newrole = role.find(:first)

object1.send(dyn\_method + ‘\<\<’, newrole)

??

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [May 27, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/dynamic-methods-for-copying-objects/138759/4 "2008-05-27T16:12:04Z")

</div>

On 27 May 2008, at 14:56, John B. wrote:

> newrole = role.find(:first)
> 
> object1.send(dyn\_method + ‘\<\<’, newrole)

It doesn’t go that far : there is no foo\<\< method (but if you’re  
looking at hashes/arrays etc… there is [] and []=  
I suppose this is because with \<\< you aren’t assigning a new  
collection, you are mutating an existing one.  
So for this, object1.send(dynmethod) \<\< newrole would do the trick

Fred
