My goal is to define contracts between classes.

I like duck typing and all but I'd like also to define an interface between different layers of my application to clearly define which are the method to call from the external, and which are accessory methods that shouldn't be used by the other layer.

For example in Java I can define a Persistor interface with methods like get() and save() and then define a JdbcPersistor class with all the methods I need to persist on database. And maybe another RestPersistor with other methods for saving on a remote restserver.

I'm not asking for interfaces in Ruby, just to know if there is a neat way do keep this distinction. I like Ruby but I worked only on small projects with it.

  • I think this belongs on StackOverflow... – thorsten müller Jan 27 '11 at 11:56
  • 2
    @thorsten Stack Overflow is for specific implementation problems (i.e. there's a problem in code). General program design questions are on-topic here on Programmers.SE. – user8 Jan 27 '11 at 11:59
  • @Mark: thanks for the correction. I thought it's too specific on Ruby (and missed Uberto's last sentence). So I updated my knowledge of the faq. (Still finding my way around here) – thorsten müller Jan 27 '11 at 12:08
  • +1 Excellent question. I'm interested if there's any other way than to write, read and keep up to date tons of documentation or to use some goofy conventions such as preceding internal accessory methods with underscores or something like that. – Joonas Pulakka Jan 27 '11 at 12:09
  • +1 By the way, PLT Racket has support for contracts, and is dynamic. Adding something similar to Ruby would be an interesting project. – Larry Coleman Jan 27 '11 at 13:34

This was answered in the context of C# interfaces and Ruby on stackoverflow: https://stackoverflow.com/questions/3505521/in-ruby-what-is-the-equivalent-to-an-interface-in-c.

Summarized: there is no exact equivalent in Ruby since duck typing makes a formal interface unnecessary. Instead, consider testing for compliance to an "interface" or contract using respond_to?.

  • 2
    yes but this is not preventing code to access method it shouldn't – Uberto Jan 27 '11 at 20:23

Some dynamically-typed languages have interfaces, or a similar concept. For example, Objective-C has protocols. But most don't. To be effective in a dynamic language, you have to forget what you know about statically-typed languages, embrace the dynamic nature of the language. Dynamically typed languages generally eschew the use of interfaces.


In Perl 5, both Moose and Moo provide roles (or traits) that can require implementation of certain methods. Moose also comes with runtime type system that allows you to define a duck_type, which is a type declaring objects having a required set of methods.

Roles are implemented by the classes (or other roles) themselves, and also provide implementation and behavior, not only the interface. But they also (at least in Moose) handle things like method conflict detection.

Not the answer you're looking for? Browse other questions tagged or ask your own question.