I come from Java and am wondering if interfaces exist in ruby? how can you mimic a contractual behavior that a Java interface provides?
For example:
interface ContactService {
public boolean successful(Contract contract);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no real concept of an interface in Ruby. Instead, people tend to just write general methods that don’t care about the type of the objects they are operating on, and just use some specific set of methods that the object will need to implement.
For example:
The
addmethod doesn’t care if its arguments are integers, strings, or arrays. They just have to be some object that implements the+operator.The
calltwicemethod doesn’t care ifobjis a lambda, proc, or some custom class. It just cares that the object has acallmethod.You can informally define an interface in the comments by telling the users of your code what methods will be called an how they should behave.