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

前端 未结 8 1394
一向
一向 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:44

    Actually in Python it's a bit harder to modify basic types.

    For example imagine, that you redefine integer.

    Ruby:

    class Fixnum 
       def *(n)
          5 
       end 
    end
    

    Now 2*2 yields 5.

    Python:

    >>> class int(int):
        def __mul__(self, x):
            return 5
    
    
    >>> 2*2
    4
    >>> int(2)*int(2)
    5
    

提交回复
热议问题