Is bad practice a monkey ruby ​​base class patch?

I am working on a ruby ​​project in which we plan to perform some operations on ruby ​​strings. Some operations are simple (for example, counting the number of words) and others are more complex (for example, checking the correctness of a given string).

A possible way to do this is to correct the String class using additional methods without changing existing methods and adding behaviors such as "some string".word_count and "some string".cjk? .

Another approach based on FileUtils is to create a class or module full of methods and always use a string as parameters, for example OddClassName.word_count("some string") and OddClassName.cjk?("some string") . We like the first one better due to readability.

I understand that a monkey correcting a base class, as described in the first version, may encounter names. However, if this is the main application, not the library, should I worry about this at all?

So the questions are:

  • Is adding methods for ruby ​​base classes a bad practice? If so, is it in all cases or only in some cases?
  • What is the best approach for this?
  • What could be the name "OddClassName"?

Please offer any options.

+4
source share
2 answers

Patching monkeys is not considered bad practice unless you write odd methods that don't have PatchedClass-related behavior (e.g. String.monkeyPatchForMakingJpegFromString pretty bad, but Jpeg.fromString is good enough.)

But if your project is large enough, the libraries that you use in it may have counter corrections, so you may have another problem with all these patch files. In Ruby 2.0, refinements come to the rescue. They work as follows: you define a module , refine your (even core) class in it, and then use this module where necessary. So in your code, it works like:

 YourClass.new.refinedMethodFromCoreClass #=> some result 

But

 CoreClass.refinedMethodFromCoreClass 

undefined method exception.

What all corrections for monkeys: the monkey patch is useful and convenient, but refinements add some functions that make your code more secure, convenient and accurate.

+4
source

I would use a new class, call it Doc or something else, because word counting and language checking sounds like document operations.

Let it take a string as a constructor parameter and have a chain of changes to return a new Doc. Also give it a to_s method that returns a string.

 class Doc def initialize(str) @str = str end def to_s @str end define word_count, cjk?, etc. end Doc.new("Some document").word_count # => 2 
+1
source

All Articles