# Ducktype, right?

**URL:** <https://rubytalk.org/t/ducktype-right/7286>\
**Category:** ruby-talk\
**Created:** [7 August 2003 21:49 UTC](https://rubytalk.org/t/ducktype-right/7286 "2003-08-07T21:49:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mills\_Thomas\_app1tam](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Mills\_Thomas\_app1tam](https://rubytalk.org/u/Mills_Thomas_app1tam)\
**Post date:** [7 August 2003 21:49 UTC](https://rubytalk.org/t/ducktype-right/7286/1 "2003-08-07T21:49:34Z")

</div>

I checked out some of the new Ruby features at [theluckystiff.net](http://theluckystiff.net) and saw  
’ducktype’ing. It sounds uber cool. Is this what is meant by ducktype?  
(The code may be inelegant; I don’t know; I’m still a Ruby Nuby). Please  
correct me if I got it wrong.

> **···**
>
> ######################################################################
> 
> # 
> 
> # ducktype
> 
> # 
> 
> # This method accepts two parameters. The first is an array of
> 
> #strings. The second is anything. As long as every string in x is the  
> #name of one of the methods provided by y, the method will return  
> #true. I grunged the return value to make sure it only returns true  
> #or false.
> 
> # 
> 
> def ducktype (x, y)  
> !(!( x.map{|f| break false if !y.respond\_to?(f) }))  
> end
> 
> #####################################################################
> 
> This is sort of like having a discovered mixin. Neat.
> 
> BTW, thanks Matz and all the contribs. This is fun!
> 
> Drew

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [7 August 2003 22:27 UTC](https://rubytalk.org/t/ducktype-right/7286/2 "2003-08-07T22:27:08Z")

</div>

More elegantly in 1.8

x.all? {|f| y.respond\_to?(f)}

martin

> **···**
>
> “Mills Thomas (app1tam)” [app1tam@ups.com](mailto:app1tam@ups.com) wrote:
> 
> > I checked out some of the new Ruby features at [theluckystiff.net](http://theluckystiff.net) and saw  
> > 'ducktype’ing. It sounds uber cool. Is this what is meant by ducktype?  
> > (The code may be inelegant; I don’t know; I’m still a Ruby Nuby). Please  
> > correct me if I got it wrong.
> > 
> > def ducktype (x, y)  
> > !(!( x.map{|f| break false if !y.respond\_to?(f) }))  
> > end

---

<div class="post-metadata">

**Author:** ![David\_A\_Black2](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@David\_A\_Black2](https://rubytalk.org/u/David_A_Black2)\
**Post date:** [7 August 2003 22:41 UTC](https://rubytalk.org/t/ducktype-right/7286/3 "2003-08-07T22:41:09Z")

</div>

Hi –

> I checked out some of the new Ruby features at [theluckystiff.net](http://theluckystiff.net) and saw  
> 'ducktype’ing. It sounds uber cool. Is this what is meant by ducktype?  
> (The code may be inelegant; I don’t know; I’m still a Ruby Nuby). Please  
> correct me if I got it wrong.

Welcome to Ruby!

I would describe “duck typing” not as a particular programming  
technique, but as a way of understanding how Ruby objects present  
themselves with regard to the notion of type. Any Ruby object has  
the potential to be customized, so to speak, as to its behavior and  
interface. So there’s a kind of circularity, when it comes to  
describing the type of an object: an object is of type  
“the type which is the type of objects that behaves the way this object  
behaves”. Hence (as I understand it) the idea of “duck typing”: if it  
behaves (walks, quacks, etc.) like a duck, then it’s a duck.

(I guess one could take circularity even further and say: if it walks  
and quacks like a duck, then it can be said to walk and quack like a  
duck 🙂

[…]

> This is sort of like having a discovered mixin. Neat.  
> ^^^^^^^^^^^^^^^^^^

Intriguing phrase. What do you mean exactly? 🙂

David

> **···**
>
> On Fri, 8 Aug 2003, Mills Thomas (app1tam) wrote:
> 
> –  
> David Alan Black  
> home: [dblack@superlink.net](mailto:dblack@superlink.net)  
> work: blackdav@shu.edu  
> Web: [http://pirate.shu.edu/~blackdav](http://pirate.shu.edu/~blackdav)

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [7 August 2003 23:14 UTC](https://rubytalk.org/t/ducktype-right/7286/4 "2003-08-07T23:14:40Z")

</div>

> I would describe “duck typing” not as a particular programming  
> technique, but as a way of understanding how Ruby objects present  
> themselves with regard to the notion of type. Any Ruby object has  
> the potential to be customized, so to speak, as to its behavior and  
> interface. So there’s a kind of circularity, when it comes to  
> describing the type of an object: an object is of type  
> “the type which is the type of objects that behaves the way this object  
> behaves”. Hence (as I understand it) the idea of “duck typing”: if it  
> behaves (walks, quacks, etc.) like a duck, then it’s a duck.

Thanks for saying this so well, David. I was trying to  
collect my thoughts, but I think you just collected them. 🙂

> > This is sort of like having a discovered mixin. Neat.  
> > ^^^^^^^^^^^^^^^^^^
> 
> Intriguing phrase. What do you mean exactly? 🙂

Yeah, I wondered that, too. Original poster: Please elaborate…

When I was working on \_The Ruby Way\_ and gathering quotations to intersperse in the material -- and I admit some of these are kind of hokey -- I was thumbing through the writings of Confucius. No, seriously. And I found a quote that I almost used, that reminded me of duck typing: "If an urn does not have the properties of an urn, can it truly be said to be an urn?" :) That may be wrong, it's from memory. Intriguing, though, isn't it? Predates Bishop Berkely, John Locke, Immanuel Kant by 2000 years. Perhaps even predates Plato. My history isn't that good.

Hal

> **···**
>
> ----- Original Message -----  
> From: [dblack@superlink.net](mailto:dblack@superlink.net)  
> To: “ruby-talk ML” [ruby-talk@ruby-lang.org](mailto:ruby-talk@ruby-lang.org)  
> Sent: Thursday, August 07, 2003 5:41 PM  
> Subject: Re: Ducktype, right?
> 
> –  
> Hal Fulton  
> [hal9000@hypermetrics.com](mailto:hal9000@hypermetrics.com)

---

<div class="post-metadata">

**Author:** ![Florian\_Frank2](https://avatars.discourse-cdn.com/v4/letter/f/a587f6/32.png) [@Florian\_Frank2](https://rubytalk.org/u/Florian_Frank2)\
**Post date:** [8 August 2003 17:19 UTC](https://rubytalk.org/t/ducktype-right/7286/5 "2003-08-08T17:19:25Z")

</div>

[…]

> x.all? {|f| y.respond\_to?(f)}

Some time ago I came up with a very similiar idea to check the interface  
of an object in a case statement without using its class constant. The  
advantage is that you can bind the created interface to a variable and  
pass it around.

module Kernel

```
def Interface(*selectors)
    interface = Object.new
    interface.instance_eval do
        @selectors = selectors
        def ===(obj) @selectors.all? { |s| obj.respond_to?(s) } end
    end
    interface
end

```

end

has\_split = Interface(:split)  
[%w[foo], “bar”, “baz”, 23 ].each do |thingy|  
print "#{thingy}: "  
case thingy  
when Interface(:join, :to\_a)  
puts “join & to\_a”  
when “baz”  
puts “is baz”  
when has\_split  
puts “split”  
else  
puts “nothing”  
end  
end

This prints:  
foo: join & to\_a  
bar: split  
baz: is baz  
23: nothing

> **···**
>
> On 2003-08-08 07:27:08 +0900, Martin DeMello wrote:
> 
> –  
> si tacuisses, philosophus mansisses.  
> – Boethius, Trost der Philosophie

---

<div class="post-metadata">

**Author:** ![Bermejo\_Rodrigo](https://avatars.discourse-cdn.com/v4/letter/b/e56c9b/32.png) [@Bermejo\_Rodrigo](https://rubytalk.org/u/Bermejo_Rodrigo)\
**Post date:** [8 August 2003 00:08 UTC](https://rubytalk.org/t/ducktype-right/7286/6 "2003-08-08T00:08:49Z")

</div>

> When I was working on \_The Ruby Way\_ and gathering quotations to intersperse in the material -- and I admit some of these are kind of hokey -- I was thumbing through the writings of Confucius. No, seriously. And I found a quote that I almost used, that reminded me of duck typing: "If an urn does not have the properties of an urn, can it truly be said to be an urn?" :) That may be wrong, it's from memory. Intriguing, though, isn't it? Predates Bishop Berkely, John Locke, Immanuel Kant by 2000 years. Perhaps even predates Plato. My history isn't that good.

> > “If an urn does not  
> > have the properties of an urn, can it truly be said to be  
> > an urn?”

A good reading about it =\>Metaphysics by Aristotle  
in last days I was reading it and I was so amazed how it can be applied  
on programming (OOP)…

```
* (1) *matter*, the substratum which makes possible the new production;
* (2) *form*, any actual determination of that substratum;
* (3) *potency*, with two significations -- as the immanent power of
  the seed to develop itself (active potency) and as a capacity of
  the seed to receive the successive forms of development (passive
  potency);
* (4) *act*, any actual determination of the process of development
  (in this signification, act is the same as form).

```

_The Four Causes_: The process of generation (and the same can be said  
of the process of an artificial production) shows the presence of four  
causes:

```
* an *efficient cause* (the generator or artist);
* a *material cause* (the organic matter or the marble);
* a *formal cause* (the form of species or the idea of the artist);
* a *final cause* (the purpose which directs the entire series of
  motions until the new organism or statue is produced).

```

ronnie.

---

<div class="post-metadata">

**Author:** ![Mike\_Stok](https://avatars.discourse-cdn.com/v4/letter/m/50afbb/32.png) [@Mike\_Stok](https://rubytalk.org/u/Mike_Stok)\
**Post date:** [8 August 2003 00:27 UTC](https://rubytalk.org/t/ducktype-right/7286/7 "2003-08-08T00:27:27Z")

</div>

In article [008e01c35d3a$cd213de0$0300a8c0@austin.rr.com](mailto:008e01c35d3a$cd213de0$0300a8c0@austin.rr.com),

> When I was working on \_The Ruby Way\_ and gathering quotations to intersperse in the material -- and I admit some of these are kind of hokey -- I was thumbing through the writings of Confucius. No, seriously. And I found a quote that I almost used, that reminded me of duck typing: "If an urn does not have the properties of an urn, can it truly be said to be an urn?" :) That may be wrong, it's from memory. Intriguing, though, isn't it? Predates Bishop Berkely, John Locke, Immanuel Kant by 2000 years. Perhaps even predates Plato. My history isn't that good.

I think he only just pre-dates Plato (427-347 B.C.) as some people put  
Confucius at around 551-479 B.C.

Quine may have an intersting take on what makes something an object, but  
he was another latecomer and we may need to do a little species  
shuffling:

[ “There’s a rabbit”,  
“There we have a rabbit”,  
“Lo! a rabbit”,  
“Lo! rabbithood again”  
].map { |phrase|  
puts phrase.gsub(‘rabbit’, ‘duck’)  
}

Mike

> **···**
>
> ## Hal E. Fulton [hal9000@hypermetrics.com](mailto:hal9000@hypermetrics.com) wrote:
> 
> [mike@stok.co.uk](mailto:mike@stok.co.uk) | The “`Stok’ disclaimers” apply.  
> [http://www.stok.co.uk/~mike/](http://www.stok.co.uk/~mike/) | GPG PGP Key 1024D/059913DA  
> [mike@exegenix.com](mailto:mike@exegenix.com) | Fingerprint 0570 71CD 6790 7C28 3D60  
> [http://www.exegenix.com/](http://www.exegenix.com/) | 75D2 9EC4 C1C0 0599 13DA

---

<div class="post-metadata">

**Author:** ![Mark\_Wilson](https://avatars.discourse-cdn.com/v4/letter/m/a3d4f5/32.png) [@Mark\_Wilson](https://rubytalk.org/u/Mark_Wilson)\
**Post date:** [8 August 2003 02:36 UTC](https://rubytalk.org/t/ducktype-right/7286/8 "2003-08-08T02:36:47Z")

</div>

[snip]

> When I was working on _The Ruby Way_ and gathering quotations  
> to intersperse in the material – and I admit some of these  
> are kind of hokey – I was thumbing through the writings of  
> Confucius. No, seriously. And I found a quote that I almost  
> used, that reminded me of duck typing: “If an urn does not  
> have the properties of an urn, can it truly be said to be  
> an urn?” 🙂 That may be wrong, it’s from memory. Intriguing,  
> though, isn’t it? Predates Bishop Berkely, John Locke,  
> Immanuel Kant by 2000 years. Perhaps even predates Plato.  
> My history isn’t that good.  
>   
> [snip]

Confucius died in 479 B.C. and Plato was born in 428 B.C., so yes he  
predates Plato.

Other quotes from The Analects:

1. Lord Chi Wen thought three times before taking any action. When the  
Master heard this, he said: “Twice is plenty enough.”

[Agile Programming]

1. The Master said: “Language is insight itself.”

[Ruby]

1. The Master said: “If things far away don’t concern you, you’ll soon  
mourn things close at hand.”

[Lesson learned, September 11, 2001]

Regards,

Mark

> **···**
>
> On Thursday, August 7, 2003, at 07:14 PM, Hal E. Fulton wrote:

---

<div class="post-metadata">

**Author:** ![Pierre-Charles\_David](https://avatars.discourse-cdn.com/v4/letter/p/71e660/32.png) [@Pierre-Charles\_David](https://rubytalk.org/u/Pierre-Charles_David)\
**Post date:** [8 August 2003 06:28 UTC](https://rubytalk.org/t/ducktype-right/7286/9 "2003-08-08T06:28:26Z")

</div>

“Bermejo, Rodrigo” [rodrigo.bermejo@ps.ge.com](mailto:rodrigo.bermejo@ps.ge.com) writes:

> > > “If an urn does not  
> > > have the properties of an urn, can it truly be said to be  
> > > an urn?”
> 
> A good reading about it =\>Metaphysics by Aristotle  
> in last days I was reading it and I was so amazed how it can be applied  
> on programming (OOP)…
> 
> ```
> * (1) *matter*, the substratum which makes possible the new production;
> * (2) *form*, any actual determination of that substratum;
> * (3) *potency*, with two significations -- as the immanent power of
> the seed to develop itself (active potency) and as a capacity of
> the seed to receive the successive forms of development (passive
> potency);
> * (4) *act*, any actual determination of the process of development
> (in this signification, act is the same as form).
> 
> ```

This relation between OO and Aristotle’s view of the world has been  
explored in a very nice and original paper at OOPLSA’2000:

@InProceedings{ rayside-campbell.oopsla2000,  
author = {Derek Rayside and Gerard T. Campbell},  
title = {An Aristotelian Understanding of Object-Oriented  
Programming},  
booktitle = {Proceedings of the 15th {ACM} Conference on  
Object-Oriented Programming, Systems, Languages, and  
Applications (OOPSLA 2000)},  
pages = {337–353},  
year = 2000,  
editor = {Doug Lea},  
address = {Minneapolis, Minnesota, USA},  
month = oct,  
organization = {ACM},  
publisher = {ACM Press},  
keywords = {oo}  
}

The PDF is available on the author’s web page (just google for the title).

> **···**
>
> –  
> Pierre-Charles David (pcdavid emn fr)  
> Computer Science PhD Student, École des Mines de Nantes, France  
> Homepage: [http://pcdavid.net/](http://pcdavid.net/)
