Conflicts between using respond_to? and extending Ruby

On Oct 22, 2007, at 3:52 PM, Rick DeNatale wrote:

Which is functionally equivalent to
donald.is_a? CanQuack

it might read better, but I still maintain that checking with is_a? is
still weaker than most folks assume.


Rick DeNatale

as i was mentioning to david, my experience is that it just depends:
if you are writing something like

case conn
when TCPSocket
when UDPSocket
end

then is_a? is probably pretty strong - as strong as it possible in
ruby since we can always

def class() Lie end

nevertheless my general rule tends to be along the lines of using
class/module/is_a? type checks for built-ins since people who muck
that too much get what they deserve if those classes stop working,
using ‘respond_to?’ to keep apis flexible (require as little as
possible from client code), and using something like
Object.instance_method(:instance-_eval).bind(object).call(*a, &b)
when trying to write robust meta-programming type stuff like adding
methods to object since i want to force compliance iff possible.

this whole thread is pretty interesting though - it shows how
flexible we as programmers have to be to accommodate the openness
ruby. i think some people would point out this as a weakness because
we have to think and talk about these things but, in reality, it’s
only an issue in pretty sticky situations - situations i personally
don’t attempt to get myself in using less powerful languages.

the language choice does indeed change the way we think about
problems and, as such, cannot truly be equivalent for a given task
when compared to other languages - contrary to what turing would tell
us :wink:

cheers.

a @ http://codeforpeople.com/

On Oct 22, 2007, at 4:34 PM, David A. Black wrote:

I’ve always thought that the situation with programming languages is a
lot like that with musical instruments: they (languages or
instruments) all do the same thing (implement algorithms or make
music), but there’s no denying, in either case, that different ones
have very different properties and provide very different experiences.

ot - but you’ll enjoy

Via 'Musicophilia,' Sacks Studies Music and the Brain : NPR

cheers

a @ http://codeforpeople.com/

On Oct 22, 6:12 pm, “Rick DeNatale” [email protected] wrote:

For instance, playing a brass or woodwind instrument simply requires a
rest every now and then.

I’m not sure what the programming language analog is, maybe memory
management in C or something.

On 10/22/07, David A. Black [email protected] wrote:

I’ve always thought that the situation with programming languages is a
lot like that with musical instruments: they (languages or
instruments) all do the same thing (implement algorithms or make
music), but there’s no denying, in either case, that different ones
have very different properties and provide very different experiences.

Not to mention different, and sometimes VERY different techniques to
‘play’ them.

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On 10/23/07, Yossef M. [email protected] wrote:

‘play’ them.

For instance, playing a brass or woodwind instrument simply requires a
rest every now and then.

I was thinking more along the lines like you don’t play a kettledrum
with a guitar pick, or a guitar with mallets. (At least not usually).

I’m not sure what the programming language analog is, maybe memory
management in C or something.

A better analogy here might be playing a woodwind, where you have to
do your own “breath management” vs. playing a pipe organ, which has a
“breath collector!”


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Hi –

On Wed, 24 Oct 2007, Rick DeNatale wrote:

Not to mention different, and sometimes VERY different techniques to
‘play’ them.

For instance, playing a brass or woodwind instrument simply requires a
rest every now and then.

I was thinking more along the lines like you don’t play a kettledrum
with a guitar pick, or a guitar with mallets. (At least not usually).

I think I took the bit about hitting a clarinet with a violin bow, or
a basketball with a golf club, out of my “Coming from Ruby” article
before it saw the light of day, but there are definitely some amusing
possibilities there :slight_smile:

I’m not sure what the programming language analog is, maybe memory
management in C or something.

A better analogy here might be playing a woodwind, where you have to
do your own “breath management” vs. playing a pipe organ, which has a
“breath collector!”

What about the old days, when you’d have an apprentice working the
bellows while you played?

David

On Oct 22, 5:09 pm, “ara.t.howard” [email protected] wrote:

def class() Lie end

Maybe I’m misunderstanding your point, but that doesn’t do much.

x = [1,2,3]
=> [1, 2, 3]
x.is_a?(Array)
=> true
x.class
=> Array
def x.class() String; end
=> nil
x.class
=> String
x.is_a?(Array)
=> true
case x
when Array then ‘arr’
when String then ‘str’
else ‘blah’
end
=> “arr”

I ran into this when I thought I was going to be able to use mock/stub
objects and handle is_a?/kind_of?/&c calls by stubbing the response to
class.