# Inherit, Delegate, or Forwarding?

**URL:** <https://www.ruby-forum.com/t/inherit-delegate-or-forwarding/51672>\
**Category:** Ruby\
**Created:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/inherit-delegate-or-forwarding/51672 "2006-01-05T19:57:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [January 5, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/inherit-delegate-or-forwarding/51672/1 "2006-01-05T19:57:31Z")

</div>

When do you guys choose to inherit from a class, use a delegate, or  
forwarding?  
What are some examples of common uses for Delegates or Forwardable?

I have these fuzzy notions of times they may be useful, but some common  
examples would be nice.

Any thought?  
.adam

For reference:  
[Forwardable]  
[http://ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/forwardable/rdoc/index.html)  
[Delegate] [http://ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/delegate/rdoc/index.html)

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [January 5, 2006, 11:24pm UTC](https://www.ruby-forum.com/t/inherit-delegate-or-forwarding/51672/2 "2006-01-05T23:24:52Z")

</div>

On Jan 5, 2006, at 12:12 PM, Adam S. wrote:

> When do you guys choose to inherit from a class, use a delegate, or  
> forwarding?

As the saying goes, “Favor composition over inheritance.” The theory  
there is simply that inheritance is a tighter coupling than  
composition, so the latter handles change better. Delegation is a  
form of composition.

Any Design Patterns book will have a detailed treatment of this very  
topic.

> What are some examples of common uses for Delegates or Forwardable?

Well, I wrote the documentation for both, and I must confess that I  
still rarely use Delegate. It can think of one example where I  
wanted full delegation and I wanted it for two classes, so I had to  
roll my own anyway. The best example I can think of for it is  
TempFile[1], in the standard library.

I use forwardable all the time, on the other hand. I love that  
library. It allows me to control what gets delegated and rename  
delegated methods. Good stuff. Two examples from my work would be  
my solution-in-progress to the Ruby Test First Challenge[2] and my  
FasterCSV library[3].

Hope that helps.

1: [http://stonecode.org:3000/articles/show\_all](http://stonecode.org:3000/articles/show_all)  
2: [http://groups.google.com/group/Ruby-Test-First-Challenge/msg/](http://groups.google.com/group/Ruby-Test-First-Challenge/msg/)  
1df37a6f0b781a88  
3: [http://rubyforge.org/plugins/scmsvn/viewcvs.php/trunk/lib/](http://rubyforge.org/plugins/scmsvn/viewcvs.php/trunk/lib/)  
faster\_csv.rb?root=fastercsv&view=markup

James Edward G. II

---

<div class="post-metadata">

**Author:** ![asetzler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ac91a4/32.png) [@asetzler](https://www.ruby-forum.com/u/asetzler)\
**Post date:** [January 6, 2006, 12:00am UTC](https://www.ruby-forum.com/t/inherit-delegate-or-forwarding/51672/3 "2006-01-06T00:00:55Z")

</div>

James Edward G. II wrote:

> I use forwardable all the time, on the other hand. I love that  
> library. It allows me to control what gets delegated and rename  
> delegated methods. Good stuff. Two examples from my work would be my  
> solution-in-progress to the Ruby Test First Challenge[2] and my  
> FasterCSV library[3].

Note that, with some help, I revamped forwardable.rb and it has been  
tentatively accepted by Matz for 2.0. The current version has lots of  
warning  
spewage.

[http://rubyforge.org/tracker/index.php?func=detail&aid=1923&group\_id=426&atid=1700](http://rubyforge.org/tracker/index.php?func=detail&aid=1923&group_id=426&atid=1700)

Regards,

Dan
