From the course: Complete Guide to Ruby

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Mixins

Mixins

- [Instructor] In this movie, we will learn how to use modules as mixins. A mixin is what Ruby programmers call a module when it's used to share attributes and methods with one or more classes. We learned how to use class inheritance to share attributes and behaviors from one class to another, but in Ruby, a subclass can only inherit from one superclass. That can be limiting sometimes. Mixins serve an important role because they give us another way to share attributes and methods. They act as a wrapper around code that several classes can share. They're named mixins because they can be mixed in to any class. You might think of them like a class fragment. Defining code in one place that gets used in many places helps keep code well organized and follows the best practice of not repeating yourself. Let's say that a project has a Person class. It has attributes for first and last name, city, state, and zip. It has methods that will join those attributes together and return the result…

Contents