How to declare what need for include ruby module -
ruby doesn't have interfaces, how tell other programmers need include current module in class, instance variables, methods, constants, etc?
there existing mixins, classes, , methods in ruby core library have exact same problem, e.g. enumerable, comparable, range, hash, array#uniq: require behavior other objects in order work. examples are:
-
the class must provide method
each, yields successive members of collection. ifenumerable#max,#min, or#sortused, objects in collection must implement meaningful<=>operator […] -
the class must define
<=>operator, compares receiver against object, returning -1, 0, or +1 depending on whether receiver less than, equal to, or greater other object. if other object not comparable<=>operator should return nil. -
ranges can constructed using objects can compared using
<=>operator. methods treat range sequence (#each, methods inheritedenumerable) expect begin object implementsuccmethod return next object in sequence.step,include?methods require begin object implementsuccor numeric. hash:a user-defined class may used hash key if
hash,eql?methods overridden provide meaningful behavior.and in order define "meaningful behavior" means, documentation of
hashfurther links documentation ofobject#hash,object#eql?:-
[…] function must have property
a.eql?(b)impliesa.hash == b.hash. […] -
[…]
eql?method returns true if obj , other refer same hash key. […]
so, can see, question quite common one, , answer is: documentation.
Comments
Post a Comment