In Ruby, what is the equivalent to an interface in C#?
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.
What is java interface equivalent in Ruby?
Ruby has Interfaces just like any other language.
Note that you have to be careful not to conflate the concept of the Interface, which is an abstract specification of the responsibilities, guarantees and protocols of a unit with the concept of the interface
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.
It is very important to distinguish the two. What's important is the Interface, not the interface
. The interface
tells you pretty much nothing useful. Nothing demonstrates this better than the marker interfaces in Java, which are interfaces that have no members at all: just take a look at java.io.Serializable
and java.lang.Cloneable
; those two interface
s mean very different things, yet they have the exact same signature.
So, if two interface
s that mean different things, have the same signature, what exactly is the interface
even guaranteeing you?
Another good example:
package java.util;
interface List<E> implements Collection<E>, Iterable<E> {
void add(int index, E element)
throws UnsupportedOperationException, ClassCastException,
NullPointerException, IllegalArgumentException,
IndexOutOfBoundsException;
}
What is the Interface of java.util.List<E>.add
?
- that the length of the collection does not decrease
- that all the items that were in the collection before are still there
- that
element
is in the collection
And which of those actually shows up in the interface
? None! There is nothing in the interface
that says that the Add
method must even add at all, it might just as well remove an element from the collection.
This is a perfectly valid implementation of that interface
:
class MyCollection<E> implements java.util.List<E> {
void add(int index, E element)
throws UnsupportedOperationException, ClassCastException,
NullPointerException, IllegalArgumentException,
IndexOutOfBoundsException {
remove(element);
}
}
Another example: where in java.util.Set<E>
does it actually say that it is, you know, a set? Nowhere! Or more precisely, in the documentation. In English.
In pretty much all cases of interfaces
, both from Java and .NET, all the relevant 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.
Note that there are other languages in which the Interface can actually be described in a meaningful way. However, those languages typically don't call the construct which describes the Interface "interface
", they call it type
. In a dependently-typed programming language, you can, for example, express the properties that a sort
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.
So, in short: Ruby does not have an equivalent to a Java interface
. It does, however, have an equivalent to a Java Interface, and it's exactly the same as in Java: documentation.
Also, just like in Java, Acceptance Tests can be used to specify Interfaces as well.
In particular, in Ruby, the Interface of an object is determined by what it can do, not what class
is is, or what module
it mixes in. Any object that has a <<
method can be appended to. This is very useful in unit tests, where you can simply pass in an Array
or a String
instead of a more complicated Logger
, even though Array
and Logger
do not share an explicit interface
apart from the fact that they both have a method called <<
.
Another example is StringIO
, which implements the same Interface as IO
and thus a large portion of the Interface of File
, but without sharing any common ancestor besides Object
.
Is a Ruby module equivalent to a Java Interface?
I think I'd equate a module to something more akin to an extension method in C#. You're adding functionality to an existing class that is actually defined elsewhere. There isn't an exact analog in either C# or Java, but I definitely wouldn't think of it as an interface because the implementation is derived as well as the interface.
Is there a C# equivalent to Ruby's `respond_to?`?
You could check the methods available on the object being wrapped using Reflection, at construction time.
Just call Type.GetMethods() on the interface and the type being passed in, and make sure the appropriate methods exist.
Edit:
As suggested by itowlson, there is an option for handling dynamic types, as well. If you check for the existance of the IDynamicMetaObjectProvider interface on the passed object, you could then call IDynamicMetaObjectProvider.GetMetaObject().GetDynamicMemberNames(), and use this information.
If the interface does not exist, you could just revert to Type.GetMethods().
This should handle the "dynamic" types, as well.
Interface in a dynamic language?
I guess there is no single answer for all dynamic languages. In Python, for instance, there are no interfaces, but there is multiple inheritance. Using interface-like classes is still useful:
- Interface-like classes can provide default implementation of methods;
- Duck-typing is good, but to an extent; sometimes it is useful to be able to write
isinstance(x, SomeType)
, especially whenSomeType
contains many methods.
Creating an abstract api over multiple web services
To my knowledge, there is no such thing as an interface in Ruby, in the sense that is used in statically typed languages like C#, Java, Objective-C or Swift. In Ruby one can not declare an abstract entity with a set of properties and methods. Nor can you force a Ruby class conform to such abstract entity. Instead, in Ruby people usually write unit tests to make sure a class behaves as expected. So a unit test in Ruby kinda acts like an interface, it documents the endpoints for a class (methods and properties) and makes sure those endpoints work.
Here is my workflow when I am building an API written in Ruby on Rails for an iOS app.
The first thing I usually do is writing a documentation for this API. Documentation is written in human friendly language and hosted in Github wiki, for example. For each HTTP endpoint I document its request URL and method, parameters, their types, provide some examples of the JSON data sent and received.
Next, I start developing this API in Ruby on Rails using TDD approach, where I first write a unit test according to the documentation that I've just written.
Finally, I write the application logic, make sure the tests pass.
On the client side (an iOS app in my case) I also write integration tests, to make sure the app works with the server API in sweet harmony.
See these SO questions:
Why do Ruby people say they don't need interfaces?
What is java interface equivalent in Ruby?
In Ruby, what is the equivalent to an interface in C#?
Interface vs Base class
Let's take your example of a Dog and a Cat class, and let's illustrate using C#:
Both a dog and a cat are animals, specifically, quadruped mammals (animals are waaay too general). Let us assume that you have an abstract class Mammal, for both of them:
public abstract class Mammal
This base class will probably have default methods such as:
- Feed
- Mate
All of which are behavior that have more or less the same implementation between either species. To define this you will have:
public class Dog : Mammal
public class Cat : Mammal
Now let's suppose there are other mammals, which we will usually see in a zoo:
public class Giraffe : Mammal
public class Rhinoceros : Mammal
public class Hippopotamus : Mammal
This will still be valid because at the core of the functionality Feed()
and Mate()
will still be the same.
However, giraffes, rhinoceros, and hippos are not exactly animals that you can make pets out of. That's where an interface will be useful:
public interface IPettable
{
IList<Trick> Tricks{get; set;}
void Bathe();
void Train(Trick t);
}
The implementation for the above contract will not be the same between a cat and dog; putting their implementations in an abstract class to inherit will be a bad idea.
Your Dog and Cat definitions should now look like:
public class Dog : Mammal, IPettable
public class Cat : Mammal, IPettable
Theoretically you can override them from a higher base class, but essentially an interface allows you to add on only the things you need into a class without the need for inheritance.
Consequently, because you can usually only inherit from one abstract class (in most statically typed OO languages that is... exceptions include C++) but be able to implement multiple interfaces, it allows you to construct objects in a strictly as required basis.
Related Topics
Pass C# String to C++ and Pass C++ Result (String, Char*.. Whatever) to C#
Understanding Floating Point Problems
C# Equivalent to Java's Charat()
An Attribute Argument Must Be a Constant Expression, ...- Create an Attribute of Type Array
How to Serialize/Deserialize a Dictionary with Custom Keys Using JSON.Net
How to Exit a Wpf Application Programmatically
Automatically Inotifypropertychanged
How to Intercept All Key Events, Including Ctrl+Alt+Del and Ctrl+Tab
When Using Iis Express No CSS Applied on a Remote Pc
How to Call an ASP.NET C# Method Using JavaScript
Does Java Have Something Similar to C# Properties
Simple Injector Unable to Inject Dependencies in Web API Controllers