Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

understanding comparable mixin and enumerable mixin

I am a newbie and learning ruby. Would like to have a better understanding of the question asked. I don't understand the use of comparable mixin and enumerable mixin. I mean we don't include these in our class when we need to use them, right? if we want to compare two objects we simply write x > y. Then what is the use of explicitly using them?

like image 981
Akash Soti Avatar asked Jul 31 '12 11:07

Akash Soti


People also ask

What is a mixin?

This article is about the programming concept. For the ice cream, see Mix-in. In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depends on the language.

Can a flavor have multiple instances of the same mixin?

The mixin usually does not have direct instances. Since a Flavor can inherit from more than one other Flavor, it can inherit from one or more mixins. Note that the original Flavors did not use generic functions.

What are the advantages of using mixins?

Mixins allow inheritance and use of only the desired features from the parent class, not necessarily all of the features from the parent class.

What is a mixin class in Entity Framework?

The mixin class extends Entity, which is the same superclass as our target class. This is important in order to preserve the semantics of any static bindings which are compiled into our mixin class. More details on this later.


2 Answers

Great Question Akash!

Sometimes it's not "simple" how two objects can be compared! What if you have a Dog class? How do you compare two Dog instances? What should be the comparison based on? Is it enough to compare their name? their breed? their DNA? It's really upto you. And thats when you can include Comparable in your model and implement the minimum function necessary yourself to define what makes the two Dog instances the same. You define the comparision. Once you have defined the <=> comparator in your module, your object can then be compared for equality or be sorted or ordered because ruby will know HOW to compare one instance against another.

Similarly, including the Enumerable module allows your class the ability to iterate over a collection of its instances. Once you implement the each method in your class, you get the whole Enumerable module's methods available in your class. Methods such as map/collect etc can be used on your class.

class Dog
  include Enumerable

  attr_accessor :puppies, :name

  def initialize(name)
    @name = name
    @puppies = []
  end

  def each(&block)
    @puppies.each do |puppy|
      puts "yielding #{puppy}"
      yield(puppy)
      puts "just yielded #{puppy}"
    end
  end

end


tommy = Dog.new("tommy")
tommy.puppies = ["julie","moti","husky"]

tommy.each do |p|
  puts p
end

big_puppies = tommy.map{|x| x.titleize }
like image 189
Aditya Sanghi Avatar answered Sep 23 '22 17:09

Aditya Sanghi


The point of both of these mixins is that they give you a whole bunch of methods while only having to implement one method yourself.

Without the Comparable mixin you'd want to define >,<, >=, <= and == on your class, whereas if you include Comparable you only need to define <=>. Comparable contains implementations of those other methods, based on your <=> method.

Similarly with enumerable you only need to define each and in return you get map, inject, partition, reject etc...

like image 39
Frederick Cheung Avatar answered Sep 21 '22 17:09

Frederick Cheung