2

I am coming from c#/.net/asp.net mvc/nancyfx and I am considering Rails for an upcoming project.

The project involves the creation of a unified/abstract api that pulls a couple of similar web services together, allowing the developer to provide implementations of each services while also interacting with them through a single api. Much like Ruby DBI does for databases.

In c# I would create a interface to define the contract all service implementations would have to adhere to (i am using online file storage services as an example to explain the issue):

public interface IStorageProvider
{
    ICollection<string> ListFilenames(string folder);
}

Then I would create implementations for each service, e.g. dropbox:

public DropboxStorageProvider : IStorageProvider
{
    public ICollection<string> ListFilenames(string folder)
    {
        var filenames = service.SomeCodeToGetFilenames(folder);
        return filenames;
    }
}

What would be the Ruby approach to create such an abstraction? Thanks!

Corstiaan
  • 1,114
  • 15
  • 34

1 Answers1

1

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.

  1. 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.

  2. 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.

  3. Finally, I write the application logic, make sure the tests pass.

  4. 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#?

Community
  • 1
  • 1
Evgenii
  • 36,389
  • 27
  • 134
  • 170
  • 1
    Thanks! I have learned that ruby does not have any interface like features and I am not looking for the "ruby implementation of interfaces". I am looking for the best practice, ruby-wise, for my case. Any tips? :-) – Corstiaan Aug 09 '15 at 10:11
  • 1
    Thanks for updating your answers with your workflow. So, am I correct in assuming that, because Ruby is duck typed, just creating classes with exactly the same methods and properties would essentially allow me to do the same things as when using interface in c#? – Corstiaan Aug 09 '15 at 11:38
  • 1
    Yes, you can create two classes with ListFilenames method. Then, you will instantiate the objects from those classes and call ListFilenames. – Evgenii Aug 10 '15 at 00:46