# I would love to see some composition examples

**URL:** <https://metaruby.com/t/i-would-love-to-see-some-composition-examples/265>\
**Category:** Ruby Chat\
**Created:** [July 22, 2015, 1:38am UTC](https://metaruby.com/t/i-would-love-to-see-some-composition-examples/265 "2015-07-22T01:38:08Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![RedFred7](https://metaruby.com/user_avatar/metaruby.com/redfred7/32/838_2.png) [@RedFred7](https://metaruby.com/u/RedFred7)\
**Post date:** [July 22, 2015, 7:26pm UTC](https://metaruby.com/t/i-would-love-to-see-some-composition-examples/265/3 "2015-07-22T19:26:09Z")

</div>

> [@PikachuEXE](#):
>
> I lack the experience to understand "prefer composition over inheritance

This is a paradigm that’s not so much relevant in Ruby as in static languages. Here’s a crude example:

```
class Car
  attr_reader :engine
  def initialize
    @engine = 1200
  end
end

class Ford < Car
end

car = Ford.new
puts car.engine

```

My Ford needs an engine and one way of getting one is to inherit from something that has one, i.e. a Car. The trouble with this is that if you need to select which engine your Ford will have based on some conditional dynamic logic then you’re stuck. You can’t un-inherit Car and re-inherit from something else that has the engine you need (although in Ruby we can use modules and meta-programming to get round that). The alternative approach would be:.

```
class Engine
  attr_reader :capacity
  def initialize
    @capacity = 1200
  end
end

class Ford
  def engine
  	Engine.new.capacity
  end

end

car = Ford.new
puts car.engine

```

We’re now _composing_ an engine when we need it, as opposed to expecting one from a parent class. The advantage here is that can dynamically plug-in whichever class we see fit at runtime, e.g. DieselEngine or PetrolEngine. This is a choice we don’t really have with inheritance, as we need to decide whether we need to inherit from a DieselCar or a PetrolCar right back when we define our class.

When it comes to choosing which method to use, i apply the old OO principle: If Class B **is a type of** class A then use inheritance. If Class B **has an instance of** A then use composition. That would mean that, in our example, my Ford is\_a car and has\_an engine. So I would use both methods here, inheriting from Car and composing an engine.

HTH

---

_[View the full topic](https://metaruby.com/t/i-would-love-to-see-some-composition-examples/265)._
