Adding methods at runtime in javascript, ruby, and java
OK, the title is a ruse, you can't easily add methods to java classes at runtime. But I'll illustrate how to do it in javascript and ruby. For example, lets supposed we have a javascript object(function) and we want to add a say_hello method to it: var myObj = {}; myObj.say_hello() // doesn't work myObj.say_hello = function() { return "hello"; } myObj.say_hello(); //works This is because javascript treats functions as first class citizens and doesn't even bother with the concept of "classes" as something other than special functions. Same thing in ruby: myObj = Object.new myObj.say_hello # doesn't work def myObj.say_hello "hello" end myObj.say_hello # works There's a subtle difference here. The ruby syntax seems a little strange to me and it wasn't obvious how to do this. In javascript, it's very obvious that you're assigning a new function to the attribute (that you're adding). In ruby, using def in...