When do we use the ruby ​​vs module using the class?

A question like this has been asked before, but I am specifically asking about using composition as an alternative to using modular mixins.

class Helper def do_somthing end end 

If I need to β€œuse” a class but not inherit it, I simply compose it and use it.

 class MyStuff def initialize helper = Helper.new helper.do_something end end 

Why do I need a module for this:

  module Helper def do_something end end class MyStuff include Helper end 

The only difference that I see will not be many Helper objects if I use modules. But I do not see anything with a large number of objects lying around larger objects.

Also, I don't know if I need to subclass it in the future. So, how do I decide if my library users want to use the mixin module or want to use composition?

+8
inheritance ruby multiple-inheritance mixins composition
source share
4 answers

If the relationship between Helper and the MyStuff class is property, use composition . This is called a has-a relationship. For example, suppose you have a Person class and a Car class. You would use the composition because the person has a car:

 class Person def initialize @car = Car.new end end class Car def accelerate # implementation end end 

When Helper acts like MyStuff , use the mixin module . Helper , in this case takes on the role of MyStuff . This is slightly different from is-a , which implies that you must use traditional inheritance . For example, let's say we have the Person class and Sleeper : Sometimes a person plays the role of a sleeper, but other objects do the same - instances of Dog , Frog or maybe even Computer . Each of these classes represents something that can fall asleep.

 module Sleeper def go_to_sleep # implementation end end class Person include Sleeper end class Computer include Sleeper end 

Sandi Metz Practical Object Oriented Design in Ruby is a great resource for these topics.

+14
source share

This is a Duck Typing question. If you want your class to behave like Helper , you do include . If you want to encapsulate Helper behavior, the right choice is require .

By mixing Enumerable , you give your class a huge load of methods by implementing a single method each . Array wrapper you can hide the iteration from others and use it to store your data. And vice versa.

+1
source share

Mixin modules are more like multiple inheritance, so follow the usual rules for inheritance and composition - is-a or has-a. By the way, it include Helper , not require 'Helper' .

0
source share

There are a few things I can share:

  • If you need to share common behaviors in different classes and modules, you must do this in modules so that later you can just include modules wherever you want. It will also help testing, since it is already DRY.

  • In case of responsibility, you can do this in the new modules in order to more clearly understand and improve the readability of the code. This will help reduce the size of your main classes, which should be easy to track and maintain.

You may notice that include adds functionality to your instance , where extend done by Class itself.

0
source share

All Articles