I’m Wrong Because Ruby and Powershell Are Mainstream

Brad Wilson and Scott Hanselman took me to task for my comment the other day that no “mainstream” language had implemented extension methods:

How mainstream is Ruby on Rails for you? Ruby is a full fledged dynamic language. No hacks for “extension methods” (Brad)

Ya, I kind of blanched at that statement too…method_missing is pretty mainstream… (Scott)

They’re right, Ruby does support the addition (and redefinition I think) of methods on a class at any time. There’s a sample of this in the Classes and Objects chapter of Programming Ruby (aka the pick-axe book) where they add a basic documentation facility “available to any module or class” in Ruby by adding a doc instance method to the Module class.

class Module
  @@docs = Hash.new(nil)
  def doc(str)
    @@docs[self.name] = str
  end
  def Module::doc(aClass)
    # If we’re passed a class or module, convert to string
    # (‘<=’ for classes checks for same class or subtype)
    aClass = aClass.name if aClass.type <= Module
    @@docs[aClass] || “No documentation for #{aClass}”
  end
end

Given how Ruby classes are defined, I think the newly added methods have access to the private data of the class. Extension methods in C#3/VB9 only have access the public interface of the object. But that’s a fairly minor difference.

FYI, Powershell can do this as well, though not as succinctly as Ruby. Scott has an example how you can add a DatePhotoTaken property to System.IO.FileInfo using Omar Shahine’sPhotoLibrary project.

Chalk this up to my continuing ignorance of dynamic languages. I’m working on it, albeit slowly.

Comments:

The biggest questions is - why invent another language for PowerShell if there are so many great popular languages already in existance?
Well then, don't both Python and Javascript count too? You can inject new methods into any of their ordinary objects.
> The biggest questions is - why invent another language for PowerShell if > there are so many great popular languages already in existance? Try it and you'll see. I'm not aware of a language that has as wide a dynamic range as PowerShell (Please point one out if you know of one). Bruce Payette's book, PowerShell in Action provides both a great language reference as well as the reasoning behind the decisions we made in producing PowerShell (Bruce is the dev lead for the language). Jeffrey Snover [MSFT] Windows PowerShell/MMC Architect Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
orchmid, Ruby not only allows you to inject methods into objects, but into classes as well. Extension Methods in C#3/VB9 act at the class level. Javascript doesn't really have classes and I don't know Python well enough to comment. As for why to invent another language, I certainly don't think a single lanugage feature is enough to argue for or against the existence of the PowerShell language. I fraking love PowerShell, and it has nothing to do with the ability to add new methods to classes.