Why do Ruby people say they don’t need interfaces?

Does ruby ​​have anything other than other OOP languages ​​(ex: PHP) that makes interfaces useless? Does he have any kind of replacement for this?

Edit:

Some explanations:

  • In other languages ​​(for example: PHP) you do not need "interfaces" (they are not necessarily at the code level). You use them to contract to improve your software architecture. Therefore, the confirmation "in ruby ​​you do not need interfaces / in other languages ​​you need interfaces because XXX" is false.

  • No, mixins are not interfaces, they are completely different (PHP 5.4 implements mixins). Have you even used the interfaces?

  • Yes, PHP is OOP. Languages ​​evolve, welcome to the present.

+6
source share
7 answers

Well, this is the consensus that when an object is passed to Ruby, it is not type checked. Interfaces in Java and PHP are a way to confirm that an object matches a specific contract or "type" (so something could be Serializable , Authorizable , Sequential and whatever you want).

However, in Ruby there is no formalized concept of a contract, for which interfaces will play some significant role, since the correspondence of the interface is not checked in the method signatures. See, for example, Enumerable . When you mix it with your object, you are using functionality , not Declaring that your object is Enumerable . The only advantage that your object is Enumerable is that by defining each(&blk) you automatically get map , select and friends for free. You can perfectly have an object that implements all the methods provided by Enumerable , but does not mix in the module, and it will still work.

For example, for any method in Ruby that expects an I / O object, you can use something that has nothing to do with IO, and then it will explode with an error, or - if you correctly executed your IO stub - it will the work is fine even if your transferred object is not declared as "IO-ish".

The idea behind this is that the objects in Ruby did not actually glorify the hash tables with a label superimposed on them (which then have some additional tags that tell the interpreter or compiler that this object has an X interface, so it can be used in context Y), but a private person responding to messages. Therefore, if the object responds to a specific message, it completely fills out the contract, and if it does not respond to this message - well, then an error occurs.

Thus, the lack of interfaces is partially compensated by the presence of modules (which may contain the functionality you are accessing without any type of promises for the caller / consumer) and partly by the tradition of sending messages as opposed to typed dicts.

You should look at some of Jim Weirich's presentations, as he covers this topic in detail.

+11
source

Since ruby ​​is duck-typed , a separate interface is not required, but only general methods should be used for objects. Take a look at the “classic” example below:

 class Duck def move "I can waddle." end end class Bird def move "I can fly." end end animals = [] animals << Duck.new animals << Bird.new animals.each do |animal| puts animal.move end 

In this example, an “interface” is the move method, which is implemented by both the Duck and Bird classes.

+3
source

This question seems to be open, but here's my trick:

The purpose of declaring an interface is two things:

  • Announce to your future yourself or colleagues what methods this class should have.
  • Announce to the computer what methods this class should have.

If you take the second goal first, the Ruby source code will never compile, so it will never be possible to check the conformity of the interface declaration and warn the developer of any inconsistency. This means that if Ruby has built-in support for the interface, it will not be able to verify compliance until runtime, where the application will crash due to a missing implementation.

So, back to the first goal. Code readability. This may make sense, and a formal Ruby agreement with interfaces can be helpful. At the moment, you are likely to contact this with the help of comments or specifications or, as I would prefer, include a declarative module. For instance.

 module Shippable # This is an interface module. If your class includes this module, make sure it responds to the following methods # Returns an integer fixnum representing weight in grams def weight raise NotImplementedError.new end # Returns an instance of the Dimension class. def dimensions raise NotImplementedError.new end # Returns true if the entity requires special handling. def dangerous? raise NotImplementedError.new end # Returns true if the entity is intended for human consumption and thereby must abide by food shipping regulations. def edible? raise NotImplementedError.new end end class Product include Shippable end 

One way to provide this interface would be to create a specification that instantiates each class that includes the Shippable module, calls four methods, and expects them to not raise NotImplementedError .

+3
source

I'm a “man with Ruby,” and I need interfaces or something like that.

Not for enforcing a contract - because enforcing something is not very similar to Ruby, and it seems to hit the point of a dynamic language, and in any case there is no "compilation" step for enforcing it - but for documenting contracts, which may be subclasses select clients (or not, although if they don’t want, they cannot complain if the code doesn’t work).

When I ran into this problem, i.e. when I write a class or module, I expect subclasses to provide methods, I usually document methods that I believe subclasses provide as follows:

 module Enumerable def each raise NotImplementedError, "Subclasses must provide this method" end end 

This is not ideal, but it is a rather rare case, and it works for me.

+2
source

I believe because Ruby is dynamically typed, while other languages ​​are statically typed. The only reason you need to use the interface in PHP is when you use type hints when passing objects.

0
source

Ruby is very dynamic and ducky. Wouldn't that make interfaces useless or redundant? Interfaces force classes to have specific methods at compile time.

Check it out too:

http://en.wikipedia.org/wiki/Duck_typing

0
source

Depends on what you mean by interface.

If by interface you mean a specific object that exists in your language that you inherit or implement, then you do not use interfaces in a language such as ruby.

If you mean an interface, since objects have a well-documented interface, then yes, of course, objects still have well-documented interfaces, they have the attributes and methods that you expect there.

I would agree that interfaces are what exist in your mind and documentation, and not in code as an object.

-1
source

Source: https://habr.com/ru/post/900459/


All Articles