Rubies refer to mixin accessors?

Is it bad practice to reference accessors on an extended object from the mixin method? The simplest example:

module WindInstrument def play mouthpiece.blow #requires a mouthpiece end end class Saxophone attr_reader :mouthpiece def initialize @mouthpiece = Mouthpiece.new end include WindInstrument end Saxophone.new.play 

In this case, I would simply move the speaker requirement directly to the WindInstrument module, but what about in a more complex scenario, where does it really make sense for the accessor to live on the extended object? Is this just a problem of non-local separation of problems?

Mixins feel useful to add encapsulated behavior that does not require knowledge of the state of the extended object. In fact, my gut tells me that the myxin should not know any condition. If he needs knowledge of the state, I would usually return to one of two options:

  • Put the state in the class and add it through composition, not through the inheritance hierarchy. My problem is that I know that rubists have the creation of mixes that access the state state, which makes the design more understandable, although less intuitive (for me).

  • Pass the mouthpiece as a parameter to the module. Even I can say that this seems to hush up the design and feels like an abomination in a ruby ​​worldview.

    / li>

Does this code bother anyone else? I know that there are many smart people who use ruby ​​there, so I assume that my problem. What am I missing? Do I just need to relax? What would you do?

+4
source share
2 answers

I think this is the same as in monkey-patching: this is normal, but you have to make sure that there are no alternatives at first (i.e. you cannot change classes using your interface), and secondly, you should be very explicit (make sure that the documents, comments and interface mention that this method of ours is necessary and will be called) and throw out a useful error message if it isn’t

+1
source

Ruby subscribers are interfaces, not an implementation.

For example, if you call person.height_in_feet= , you do not know what units the height is actually implemented as an instance variable. It can be meters, feet or elbows.

One real-world example of mixins using an accessor is the Enumerable module. Although I do not include this module in any classes that I create, I am pleased with what it does. This gives you convenient methods like map and each_with_index , remaining DRY - there is only one implementation of which objects you refer to with all the mixee methods, and there is only one definition of what map for any object that uses Enumerable .

0
source

Source: https://habr.com/ru/post/1338924/


All Articles