Ducktype, right?

I checked out some of the new Ruby features at 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

More elegantly in 1.8

x.all? {|f| y.respond_to?(f)}

martin

···

“Mills Thomas (app1tam)” app1tam@ups.com wrote:

I checked out some of the new Ruby features at 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

Hi –

I checked out some of the new Ruby features at 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 :slight_smile:

[…]

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

Intriguing phrase. What do you mean exactly? :slight_smile:

David

···

On Fri, 8 Aug 2003, Mills Thomas (app1tam) wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

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. :slight_smile:

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

Intriguing phrase. What do you mean exactly? :slight_smile:

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
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Thursday, August 07, 2003 5:41 PM
Subject: Re: Ducktype, right?


Hal Fulton
hal9000@hypermetrics.com

[…]

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

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.

In article 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 wrote:

mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

[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?” :slight_smile: 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:

“Bermejo, Rodrigo” 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/