..


Sponsored Links

Use of mixins in Ruby

Article written by Alessio Saltarini
Page 1 of 2

Sometimes you want to create a class of objects that have "this" feature, but also "this" and "this other", which are perhaps already been written elsewhere in our source code.

In other words, we want to create a class that borrows methods and properties defined in other parts of the source code or in some library.

Conversely, we would gather around the same features our source code in one place accessible to all. How?

The purists are horrified object-oriented programming and support - in my opinion not entirely without reason - that every need of this type can be resolved with the proper use of polymorphism.

Others, especially if they come from experience with languages ​​like C + +, argue that precisely because of these requirements using multiple inheritance.

Pragmatic languages ​​like Ruby, however, address the issue by providing a technique called "mixin", the name - it sounds! - An ice cream man gave her the Massuchussets composed of various ice cream flavors (cream, chocolate ...)

The Ruby mixins

Technically, Ruby mixins are implemented using within classes, code defined in different modules (module).

To take a trivial example, suppose I wanted to implement a word processor that automatically print out my friendliness of a business letter. Since you want to enter only the necessary data, ie the recipient and the message. The program will think the different friendly like, "Sir" and "Sincerely."

Let's start by defining in a friendly form:






 module CordialitaLettera



    



 def start (address)



        



 return "Dear Mr.." + recipient + "\ n"



    



 end



    



 final signature (letter)



        



 return letter + "\ n" + "Sincerely, \ nCarlo Smith"



    



 end







 end



Nothing in particular, then, only two methods that can change the text received as input.

Let's write a class "mixed" to these methods, just a mixin:






 Class Letters



    



 includes CordialitaLettera



    



 final stampaLettera (recipient, message)



        



 = start point (receiver)



        



 = letter + post



        



 puts signature (letter)



    



 end







 end



Letters above the class has a method that takes as input only the variable parts of the letter, with friendly format defined in the module.

As you can see the syntax is quite similar to inheritance in Ruby, however, unlike the latter, in a class can implement any number of forms - and as is known, a class can be derived from a and only one parent class (Ruby does not support multiple inheritance!)

For example we can write:






 def main ()



    



 Lengin = Lettere.new



    



 lEngine.stampaLettera ('Alessio Saltarini',



                          



 'I just proceeded to transfer



                           



 you requested. ')



    



 puts



    



 lEngine.stampaLettera ('Matthew Pisconi',



                          



 'I send the fax to nr.038283873.')







 end



which returns:





 Dear sig.Alessio Saltarini,







 I've just done the transfer you requested.







 Sincerely,







 Carlo Rossi








 Dear sig.Matteo Pisconi,







 I send the fax to nr.038283873.







 Sincerely,







 Carlo Rossi



This technique, among other things, allows us to assimilate the use of mixins that of interfaces in Java: in fact, if a class is "mixing" with a form, in fact it will implement the methods (and therefore meets a certain interface) .

In the same category ...
E-Learning
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links