Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ruby has <em>Interfaces</em> just like any other language.</p> <p>Note that you have to be careful not to conflate the concept of the <em>Interface</em>, which is an abstract specification of the responsibilities, guarantees and protocols of a unit with the concept of the <code>interface</code> which is a keyword in the Java, C# and VB.NET programming languages. In Ruby, we use the former all the time, but the latter simply doesn't exist.</p> <p>It is very important to distinguish the two. What's important is the <em>Interface</em>, not the <code>interface</code>. The <code>interface</code> tells you pretty much nothing useful. Nothing demonstrates this better than the <em>marker interfaces</em> in Java, which are interfaces that have no members at all: just take a look at <a href="http://Download.Oracle.Com/javase/7/docs/api/java/io/Serializable.html" rel="noreferrer"><code>java.io.Serializable</code></a> and <a href="http://Download.Oracle.Com/javase/7/docs/api/java/lang/Cloneable.html" rel="noreferrer"><code>java.lang.Cloneable</code></a>; those two <code>interface</code>s mean <em>very</em> different things, yet they have the <em>exact same</em> signature.</p> <p>So, if two <code>interface</code>s that mean different things, have the same signature, what <em>exactly</em> is the <code>interface</code> even guaranteeing you?</p> <p>Another good example:</p> <pre><code>interface ICollection&lt;T&gt;: IEnumerable&lt;T&gt;, IEnumerable { void Add(T item); } </code></pre> <p>What is the <em>Interface</em> of <a href="http://MSDN.Microsoft.Com/library/63ywd54z.aspx" rel="noreferrer"><code>System.Collections.Generic.ICollection&lt;T&gt;.Add</code></a>?</p> <ul> <li>that the length of the collection does not decrease</li> <li>that all the items that were in the collection before are still there</li> <li>that <code>item</code> is in the collection</li> </ul> <p>And which of those actually shows up in the <code>interface</code>? None! There is nothing in the <code>interface</code> that says that the <code>Add</code> method must even <em>add</em> at all, it might just as well <em>remove</em> an element from the collection.</p> <p>This is a perfectly valid implementation of that <code>interface</code>:</p> <pre><code>class MyCollection&lt;T&gt;: ICollection&lt;T&gt; { void Add(T item) { Remove(item); } } </code></pre> <p>Another example: where in <a href="http://Download.Oracle.Com/javase/7/docs/api/java/util/Set.html" rel="noreferrer"><code>java.util.Set&lt;E&gt;</code></a> does it actually say that it is, you know, a <em>set</em>? Nowhere! Or more precisely, in the documentation. In English.</p> <p>In pretty much all cases of <code>interfaces</code>, both from Java and .NET, all the <em>relevant</em> information is actually in the docs, not in the types. So, if the types don't tell you anything interesting anyway, why keep them at all? Why not stick just to documentation? And that's exactly what Ruby does.</p> <p>Note that there are <em>other</em> languages in which the <em>Interface</em> can actually be described in a meaningful way. However, those languages typically don't call the construct which describes the <em>Interface</em> "<code>interface</code>", they call it <code>type</code>. In a dependently-typed programming language, you can for example express the properties that a <code>sort</code> function returns a collection of the same length as the original, that every element which is in the original is also in the sorted collection and that no bigger element appears before a smaller element.</p> <p>So, in short: Ruby does not have an equivalent to a Java <code>interface</code>. It <em>does</em> however have an equivalent to a Java <em>Interface</em>, and its exactly the same as in Java: documentation.</p> <p>Also, just like in Java, <em>Acceptance Tests</em> can be used to specify <em>Interface</em>s as well.</p> <p>In particular, in Ruby, the <em>Interface</em> of an object is determined by what it can <em>do</em>, not what <code>class</code> is is, or what <code>module</code> it mixes in. Any object that has a <code>&lt;&lt;</code> method can be appended to. This is very useful in unit tests, where you can simply pass in an <code>Array</code> or a <code>String</code> instead of a more complicated <code>Logger</code>, even though <code>Array</code> and <code>Logger</code> do not share an explicit <code>interface</code> apart from the fact that they both have a method called <code>&lt;&lt;</code>.</p> <p>Another example is <a href="http://RubyDoc.Info/docs/ruby-stdlib/1.9.2/StringIO/" rel="noreferrer"><code>StringIO</code></a>, which implements the same <em>Interface</em> as <code>IO</code> and thus a large portion of the <em>Interface</em> of <code>File</code>, but without sharing any common ancestor besides <code>Object</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload