One of the nicest things i found is the notion of module mixing, and although you can artificially do the same with Java and Spring (google for @DeclareParents) , it’s not as neat and clean as Ruby does it.
Module mixins basically allow us to add behaviour to our objects without inheritance. Is like if in Java we could use an Interface that has a default behaviour associated to it, so we don’t need to actually implement the methods of the interface.
The objects that has the module mixed in, can receive method calls on the module like if the methods belong to the object itself. This allows to a nice approach to domain rich programming, while at the same time maintaining a great separation of concerns and avoiding filling the classes and objects with lots of not very related functionality.
To explain the last paragraph better, let’s take a simple example. a Person class.
In our example a person can have emotions (happy, sad, etc), can do actions (play, sleep), have measure attributes and queries(height,weight). It can also be used in database operations (If we want like an ActiveRecord approach) (save, update, etc).
In a Model centric approach (Instead of using services, DAOs, etc) we would like to have Person objects that allow us to do thing like:
- if (person.sad?)
- person.playWith(toy)
- if (person.higher_than?(person2))
- person.store
We can consider each of these methods addressing different concerns, but all valid thing for a Person to know about. if we were to program this with Java, we would end up with a Person class with all these methods like:
- public class Person{
- public boolean isSsad(){...}
- public void playWith(Toy toy){...}
- public int store(){...}
- public boolean isHigherThan(Person person2) {...}
- }
We could argue that a better approach would be to have “behavioural” modules and plug them individually to our class. For example in Ruby we could have something like:
- class Person
- include Sentiments
- include Actions
- include Persistence
- include Measures
- end
We can have then each module dealing with its own concern, and making all this knowledge and capacities available to the Person objects as if they were defined on the Person class.
So for example our “Measures” module could have something like:
- module Measures
- attr_accesor :height
- def higher_than?(entity)
- return self.height > entity.height
- end
- end
Then we can do something like:
- a= Person.new
- a.height = 150
- b=Person.new
- b.height=200
- b.higher_than? a
This same approach can be used for the other modules like Persistent or Sentiments.