# Interfaces in Ruby?

**URL:** <https://www.ruby-forum.com/t/interfaces-in-ruby/49638>\
**Category:** Ruby\
**Created:** [December 7, 2005, 3:52am UTC](https://www.ruby-forum.com/t/interfaces-in-ruby/49638 "2005-12-07T03:52:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 7, 2005, 3:52am UTC](https://www.ruby-forum.com/t/interfaces-in-ruby/49638/1 "2005-12-07T03:52:18Z")

</div>

I’m new to Ruby - actually new to dynamically typed languages in  
general. I’m from more of a C++ and C# (strongly-typed and  
statically-typed) background.

One thing I’m trying to wrap my brain around is the lack of explicit  
interface definitions. In C# we use interfaces so that client code can  
program against an abstract interface, and then you can give that client  
any concrete implementation of that interface as needed. Interfaces  
provide a level of abstraction/indirection that increase reusability and  
testability.

Now, in Ruby, I like the “walk like a duck” approach. Actually, it’s  
similar to C++ templates which are very powerful.

But there are two things that I worry about as I forge ahead in my new  
Ruby lifestyle:

1. In C# I can write code that can query an object for the interfaces it  
implements to find out what I can do with it. (Rarely used but  
sometimes a lifesaver). In Ruby I guess I have to use .methods to find  
out what methods it implements?

2. In C# I can insure that a function is given an object that implements  
a certain interface. If I don’t, it’s a compile-time error and I know  
it before I ship it. In Ruby, I won’t know if I accidentally pass in  
the wrong object until runtime. I might not catch it before I ship the  
code.

Can anyone provide some enlightenment?

Thanks!  
Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 7, 2005, 4:04am UTC](https://www.ruby-forum.com/t/interfaces-in-ruby/49638/2 "2005-12-07T04:04:39Z")

</div>

On 12/6/05, Jeff C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 1. In C# I can write code that can query an object for the interfaces it  
> implements to find out what I can do with it. (Rarely used but  
> sometimes a lifesaver). In Ruby I guess I have to use .methods to find  
> out what methods it implements?

You can use Mixins (Modules) and the query the mixins.  
So if your object includes Comparable or Enumerable, you can be sure  
it has the methods of these mixins

> 1. In C# I can insure that a function is given an object that implements  
> a certain interface. If I don’t, it’s a compile-time error and I know  
> it before I ship it. In Ruby, I won’t know if I accidentally pass in  
> the wrong object until runtime. I might not catch it before I ship the  
> code.

using respond\_to? is usually the way you can do this.

See the Java / Ruby example below

[http://www.kdedevelopers.org/node/1527](http://www.kdedevelopers.org/node/1527)

> Can anyone provide some enlightenment?

Write proper unit tests so you catch it before it goes out the door 🙂

HTH,  
Greg

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 7, 2005, 5:21am UTC](https://www.ruby-forum.com/t/interfaces-in-ruby/49638/3 "2005-12-07T05:21:38Z")

</div>

On Dec 6, 2005, at 9:03 PM, Gregory B. wrote:

> On 12/6/05, Jeff C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 1. In C# I can write code that can query an object for the  
> > interfaces it  
> > implements to find out what I can do with it. (Rarely used but  
> > sometimes a lifesaver). In Ruby I guess I have to use .methods to  
> > find  
> > out what methods it implements?

We generally use respond\_to?() for this, but I must stress be REALLY,  
REALLY sure before you check. Ruby is VERY dynamic. The object may  
not have the method you ask for until you call it. This is totally  
normal and okay.

I’ve actually seen people check, so they can throw an Exception if  
it’s not there. Ruby was going to do that anyway. 😉

> > Can anyone provide some enlightenment?
> 
> Write proper unit tests so you catch it before it goes out the door 🙂

This is first class advice right here. This should catch type issues  
and much, much more. The better you get at it, the more you will  
structure your code for testing (using small methods, for example).  
That just reduces type issues even further.

And you gain so much freedom and power. I promise, when it clicks,  
you’ll never go back. “Free your mind…” 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [December 7, 2005, 3:21pm UTC](https://www.ruby-forum.com/t/interfaces-in-ruby/49638/4 "2005-12-07T15:21:04Z")

</div>

Jeff C. wrote:

> One thing I’m trying to wrap my brain around is the lack of explicit  
> interface definitions. In C# we use interfaces so that client code can  
> program against an abstract interface, and then you can give that client  
> any concrete implementation of that interface as needed. Interfaces  
> provide a level of abstraction/indirection that increase reusability and  
> testability.

As others have already noted in Ruby we generally do the simplest thing  
that will work – which is just calling methods in most cases. Where  
that doesn’t work we can still check for the presence of an included  
Module or methods. Or actually rescue the NoMethodError ourselves and  
try something else. There’s a lot of ways.

Anyway – the reason for why I’m really posting is that I have played  
around with using unit tests for doing interfaces / contracts in Ruby.  
This can provide you with argument checking and similar things, but it  
is a toy. You can find this at [http://ruby-contract.rubyforge.org/](http://ruby-contract.rubyforge.org/)

It might also give you a general idea of how much power you can get out  
of Ruby’s dynamic nature.
