ruby - Best practice: Creating a module with expectation of type its included in? -


when defining module in ruby, have seen examples module expects class(es) included in have particular definitions , functionality.

for example, module might call method doesn't contain expecting class included in contains method definition.

to me, different paradigm background of typed languages lacking ability include arbitrary set of logic (ruby whole totally different paradigm matter).

is acceptable style per ruby standards or @ least seen enough in mature code-bases considered "ok"?

i realize subjective question, i'm looking see if occurs enough 1 considered within "norm" if construct module such.

since ruby loosely typed, relying more on ability respond methods, so-called duck typing, class, it's difficult take responsibility these things.

if module included in context incompatible, it's not job module it. there's no way can know, in advance, if given method call succeed or fail because after module included there might other things happen make doing kind of validation premature.

given these limitations, it's important try , architect things avoid sort of ambiguity.

take enumerable example. in order work correctly when included, number of methods must defined in context it's included. if these not defined, module not work, responsibility failure not enumerable, module included it.

basically, before include somemodule should dependencies, if any, including module met. includer, assume responsibility conflicts might cause.

other languages have more rigid methods validating integrity, protocols feature of objective c or presence of base class in c++. ruby doesn't have this, ensure working correctly, automated test suites employed. might notice ruby community test-oriented, , sort of ambiguity 1 of many reasons why case.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -