If monkey patching is permitted in both Ruby and Python, why is it more controversial in Ruby?

前端 未结 8 1393
一向
一向 2021-02-05 12:34

In many discussions I have heard about Ruby in which people have expressed their reservations about the language, the issue of monkey patching comes up as one of their primary c

8条回答
  •  一向
    一向 (楼主)
    2021-02-05 12:37

    I think that monkey patching should only be used as the last solution.

    Normally Python programmers know how a class or a method behave. They know that class xxx is doing things in a certain way.

    When you monkey patch a class or a method, you are changing it's behavior. Other Python programmers using this class can be very surprised if that class is behaving differently.

    The normal way of doing things is subclassing. That way, other programmers know that they are using a different object. They can use the original class or the subclass if they choose to.

提交回复
热议问题