# Simple copy of attributes

**URL:** <https://www.ruby-forum.com/t/simple-copy-of-attributes/79771>\
**Category:** Ruby\
**Created:** [December 18, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/simple-copy-of-attributes/79771 "2006-12-18T22:24:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [December 18, 2006, 10:24pm UTC](https://www.ruby-forum.com/t/simple-copy-of-attributes/79771/1 "2006-12-18T22:24:33Z")

</div>

Hello !

I’ve just stumbled upon code where you have different classes with no  
relations, and that you want to copy some attributes from one to the  
other. So I came up with this:

def copy\_attributes(dest, src, \*attrs)  
for attr in attrs  
dest.send(attr.to\_s + ‘=’,src.send(attr))  
end  
end

Then, if you want, say, to copy the attributes foo, bar and baz (to  
reach summits in originality) from biniou to bidule you just need:

copy\_attributes(bidule, biniou, :foo, :bar, :baz)

Is there already a function from standard libs doing something similar  
?

I know that some of you will answer that coming to such a need means I  
should probably refactor my code. Well, as in my case I need to copy  
similar members from quite different Struct, I would say no ;-)… (and  
it’s a 100-lines script, not a big project !).

Cheers !

```
Vince
```

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [December 18, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/simple-copy-of-attributes/79771/2 "2006-12-18T22:30:35Z")

</div>

Vincent F. wrote:

> end  
> should probably refactor my code. Well, as in my case I need to copy  
> similar members from quite different Struct, I would say no ;-)… (and  
> it’s a 100-lines script, not a big project !).

My personal view is that you should create a class responsible for data  
handling and processing, and include methods capable of cloning some or  
all  
of the data members of one instance of the class to another. That way,  
you  
will have encapsulated the specifics of the data copying or processing  
within the class.

Just my personal impression, based on how you are proceeding.

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [December 18, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/simple-copy-of-attributes/79771/3 "2006-12-18T23:00:38Z")

</div>

Paul L. wrote:

> My personal view is that you should create a class responsible for data  
> handling and processing, and include methods capable of cloning some or all  
> of the data members of one instance of the class to another. That way, you  
> will have encapsulated the specifics of the data copying or processing  
> within the class.

I agree that that would be the Ruby OO way. But, let’s face it, for  
small scripts, a script-like procedural approach is much faster and  
easier to develop. For one, I discovered I would use another class just  
about when I reached the line where it became necessary…

```
Vince
```

---

<div class="post-metadata">

**Author:** ![gnuvince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/82dd89/32.png) [@gnuvince](https://www.ruby-forum.com/u/gnuvince)\
**Post date:** [December 20, 2006, 7:24pm UTC](https://www.ruby-forum.com/t/simple-copy-of-attributes/79771/4 "2006-12-20T19:24:53Z")

</div>

Vincent F. wrote:

> about when I reached the line where it became necessary…  
> In your first post, you show an example of a copy from one class  
> instance to  
> another (or so it appears). That’s why I suggested formalizing the  
> process  
> of copying within the class.

It seems a class already exists, and you are copying select values from  
one  
instance to another. It just seemed a better approach to do this copying  
activity formally within the class itself.
