“Interfaces & Abstract Classes unnecessary in Ruby” --> Can someone explain?

前端 未结 2 437
时光说笑
时光说笑 2021-02-13 00:36

I\'m trying to wrap my head around Ruby, and one thing I\'m struggling with is the lack of interface/abstract class support. From googling about, the response I continuously see

2条回答
  •  迷失自我
    2021-02-13 00:54

    I am also Ruby starter. From my understanding, there is a closer rival for abstract classes in ruby. that is module. you can't create any instances of module but you can include with another class. So a target class will get the whole functionality of parent

      module Log
        def write
          //blah
        end
      end
    
      class EventLog
        include Log
    
        def Prepare
        end
      end
    

    In statically typed languages like java/C# , Interfaces enforce the classes to have all the methods at compile time. Since Ruby is dynamic, there is no meaning in it.

    For more clarity, check these posts why dynamic languages don't require interfaces..

    1. why-dont-we-require-interfaces-in-dynamic-languages
    2. why-do-dynamic-languages-like-ruby-and-python-not-have-the-concept-of-interfaces

    Cheers

提交回复
热议问题