Trans
12/19/2006 4:29:00 PM
Vincent Fourmond wrote:
> 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 !).
Facets has Kernel#set_from:
bidule.set_from(biniou, :foo, :bar, :baz)
hth,
t.