Is it acceptable practice to patch Ruby's base classes, such as Fixnum?

后端 未结 4 1054
臣服心动
臣服心动 2020-12-31 03:47

I am still very new to Ruby (reading through the Pickaxe and spending most of my time in irb), and now that I know it\'s possible to patch classes in Ruby, I\'m

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 04:24

    The safest way is to define your own class that inherits from the built-in one, then add your new stuff to your new class.

    class MyDateTime < DateTime
      alias...
      def...
    

    But obviously now you only get the new behavior if you declare objects of your new class.

提交回复
热议问题