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:

  • enumerable:

    the class must provide method each, yields successive members of collection. if enumerable#max, #min, or #sort used, objects in collection must implement meaningful <=> operator […]

  • comparable:

    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.

  • range:

    ranges can constructed using objects can compared using <=> operator. methods treat range sequence (#each , methods inherited enumerable) expect begin object implement succ method return next object in sequence. step , include? methods require begin object implement succ or 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 hash further links documentation of object#hash , object#eql?:

  • object#hash:

    […] function must have property a.eql?(b) implies a.hash == b.hash. […]

  • object#eql?:

    […] eql? method returns true if obj , other refer same hash key. […]

so, can see, question quite common one, , answer is: documentation.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -