A common thing to do in ruby is to share functionality among classes. Since ruby doesn’t support multiple inheritance, we simply import code by mixing in modules. So instead of classes having a crazy inheritance tree, they can have includes and/or extends at the top of the class. But what is the difference between include and extend?
They both mix in functionality into a class, but they are added to different places. Include mixes functionality into instances of a class, whereas extend mixes functionality into a class itself.
Take a look at the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
And of course with ruby being dynamic and all, you can include and/or extend modules at run time
1 2 3 4 5 6 7 8 9 10 |
|
If you want more information on include and extend check out the following links: