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

后端 未结 6 1049
轮回少年
轮回少年 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 13:11

    There are no interfaces in ruby since ruby is a dynamically typed language. Interfaces are basically used to make different classes interchangeable without breaking type safety. Your code can work with every Console as long it behaves like a console which in C# means implements IConsole. "duck typing" is a keyword you can use to catch up with the dynamic languages way of dealing with this kind of problem.

    Further you can and should write unit tests to verify the behavior of your code. Every object has a respond_to? method you can use in your assert.

提交回复
热议问题