FWIW, in Javascript you can invoke a method defined anywhere with any scope that you wish. For example:
Object.prototype.toString = function(){
return '['+this.constructor.name+' "'+this.name+'"]';
}
function Bird( inName ){
this.name = inName;
this.wings = 2;
}
Bird.prototype.fly = function(){
if ( this.wings )
{
this.altitude += 100;
alert( 'Look ma, ' + this + ' is flying!' );
}
}
function Pig( inName ){
this.name = inName;
}
var robin = new Bird( 'Tweeters' );
robin.fly(); // Look ma, [Bird "Tweeters"] is flying!
var super_pig = new Pig( 'Babe' );
super_pig.wings = 17;
robin.fly.call( super_pig ); // Look ma, [Pig "Babe"] is flying!
Bird.prototype.fly.call( super_pig ); // Look ma, [Pig "Babe"] is flying!
Perhaps it's the difference between a strongly-typed dynamic language, and a loosely-typed dynamic language. Perhaps it has no place in Ruby. But on occasion, I've enjoyed this flexibility in Javascript.
What makes Ruby not amenable to running arbitrary (but predefined) code against an arbitrary scope, and deciding only during the execution of each statement if the instance variables exist or not?
···
On Jan 25, 2005, at 9:09 AM, Gennady Bystritksy wrote:
I do not think what you want has anything to do with 'duck typing'. 'duck typing' only means that you can send a message to any object and if the object has a corresponding method it gets executed regardless of object type. It does not urge you to abandon the "normal" way of method definition for a class.