Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
I think this belongs on StackOverflow... – thorsten müller Jan 27 '11 at 11:56
@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
up vote 4 down vote accepted

This was answered in the context of C# interfaces and Ruby on stackoverflow:

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

share|improve this answer
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.

share|improve this answer

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.

share|improve this answer

protected by gnat Apr 25 at 4:50

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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