Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In JavaScript I can do the following:

  • File of some framework defines a class. (It called a prototype, but in general terms it is a class anyway)
  • I can add methods in that class and overload some its methods without changing the source file with the class definition.

Can I do the same in Ruby? Python?

I'm choosing a new platform for server-side development. I need something easy for AOP.

Sample:

// Base file
function Foo() {}
Foo.prototype = {
    bar: function() {
        doTheWork();
    }
};

// Module, which wants to extend the base
(function() {
    var original = Foo.prototype.bar;
    Foo.prototype.bar = function() {
        prepareTheWork();
        orignal();
    };
})();
share|improve this question

closed as off-topic by gnat, MichaelT, Dan Pichelman, Kilian Foth, Ozz Oct 15 '13 at 11:18

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions about what language, technology, or project one should take up next are off topic on Programmers, as they can only attract subjective opinions for answers. There are too many individual factors behind the question to create answers that will have lasting value. You may be able to get help in The Whiteboard, our chat room." – Kilian Foth, Ozz
  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – gnat, MichaelT, Dan Pichelman
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I believe that is possible in Ruby, there's also an AOP framework for Ruby: aquarium.rubyforge.org –  FrustratedWithFormsDesigner Jul 15 '11 at 14:27
    
@FrustratedWithFormsDesigner: your comment is a good answer. –  kevin cline Jul 15 '11 at 14:56
    
@kevin cline: I posted as a comment beacuse I really wasn't sure, as I'm not really a Ruby programmer, just someone who thinks it's neat but never gets much chance to work in in. ;) –  FrustratedWithFormsDesigner Jul 15 '11 at 14:56
add comment

3 Answers

up vote 1 down vote accepted

I know you can in Ruby, all classes are open. So if you have class Foo in Foo.rb all you would have to do is declare the class like normal and then add your methods.

require 'Foo'
class Foo
    def addedFunctionality
          "This does nothing!"
    end
end

Hopefully that is what you are looking for.

share|improve this answer
add comment

Yes, you can do this in both Ruby and Python. I prefer Ruby, but my Python experience is limited. Programming Ruby is excellent, and contains complete, well-written documentation of Ruby meta-programming.

share|improve this answer
add comment

The functionality that you are describing can be achieved in any language: for example in Java you could define your "prototype" as a HashMap, store objects inside it that implement your function and update those function objects from another part of the code base. Not particularly elegant, but it works.....

In Clojure (my current favourite language for server-side dev) you could do:

(def base-prototype-object 
  {:bar-function-slot 
     (fn [some-parameter] (do-whatever-you-like-with some-parameter))}

;; later in the code
(def extended-object-using-prototype
  (merge 
    base-prototype
    {:bar-function-slot 
      (fn [some-parameter] (do-something-different-with some-parameter))}

Overall, I wouldn't make this capability the primary factor for choosing a language - library support, community, performance, robustness of platform, cross-platform portability, ability to leverage existing skills in the team etc. are are far more important.

share|improve this answer
    
Although this functionality is indeed implementable in any language, the HashMap-based "object" implementation is really limited for languages such as Java and C++, where this is not the usual method for writing classes and most (if not all existing code) is not written with this support in mind. If you require extending core library classes (e.g. you're doing AOP), then I would choose a language with direct or native support for this (e.g. Python, Ruby, Clojure, ...). –  André Caron Jul 15 '11 at 14:52
    
@Andre - yes I agree, prototyped-based inheritance is rather unidiomatic in statically typed languages like Java/C++. I'm only trying to make clear that it isn't impossible (which some people think....) –  mikera Jul 15 '11 at 15:00
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.