In Ruby, what is the equivalent to an interface in C#?

后端 未结 6 1041
轮回少年
轮回少年 2021-01-01 12:31

I\'m currently trying to learn Ruby and I\'m trying to understand more about what it offers in terms of encapsulation and contracts.

In C# a contract can be defined

6条回答
  •  遥遥无期
    2021-01-01 12:57

    Jorg has a good point, ruby has interfaces, just not the keyword. In reading some of the replies, I think this is a negative in dynamic languages. Instead of enforcing an interface through the language, you must create unit tests instead of having a compiler catch methods not being implemented. It also makes understanding method harder to reason about, as you have to hunt down what an object is when you are trying to call it.

    Take as an example:

    def my_func(options)
      ...
    end
    

    If you look at the function, you have no clue what options is and what methods or properties it should call, without hunting for the unit tests, other places it is called, and even look at the method. Worse yet, the method may not even use those options but pass it to further methods. Why write unit tests when this should have been caught by a compiler. The problem is you must write code differently to express this downside in dynamic languages.

    There is one upside to this though, and that is dynamic programming languages are FAST to write a piece of code. I don't have to write any interface declaration and later I can new methods and parameters without going to the interface to expose it. The trade-offs are speed for maintenance.

提交回复
热议问题