Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5459 (closed enhancement: fixed)

Opened 4 years ago

Last modified 3 years ago

[PATCH] Simple Ruby like OO support

Reported by: alex@twologic.com Assigned to: sam
Priority: high Milestone:
Component: Prototype Version:
Severity: normal Keywords:
Cc:

Description

For full details visit my blog post about the standalone library here: http://twologic.com/archives/2006/06/17/javascript-oo-ruby-style/

The following is a code snippet that showcases some of the libraries features. There are more examples over at http://www.twologic.com/repos/inheritance/examples/

Example Usage:

Debug = {
  log: function(msg) {
    document.writeln(msg);
  },
  
  inspect: function() {
    var out = '{\r'; // use /r so I can find it over /n
    for (var p in joe) {
      out += p + ": " + ((joe[p].toString()).replace(/\r/g, '\\r')) + ',\r'
    }
    out = out.substring(0, out.length - 2);
    out += '\r}'
    return out;
  }
}

Employee = Class.create({

  // constructor
  initialize: function(name, dept) {
    this.name = name;
    this.dept = dept;
  },


  doWork: function() {
    return this.name + " is now working...";
  }
});

// Manager inherits functionality from Employee
Manager = Class.create(Employee, {

  // ruby mixins; mixin all of Debug's methods
  include: Debug,
  // include: [Debug, Comparable] // would also import Comparable if it was defined

  // constructor
  initialize: function(name, dept, title) {
    this.parent(name, dept); // calls initialize on the parent, Employee
    this.title = title;
  },

  // overrideen method
  doWork: function() {
    this.log(this.parent() + "at managing the employees.");
  }
});

var joe = new Manager("Joe", "Sales", "Sales Manager");
joe.doWork()

Debug.log("Manager = " + joe.inspect());

Attachments

ruby_style_inheritance.diff (2.5 kB) - added by alex@twologic.com on 06/22/06 07:38:26.
javascript OO ruby stle
ruby_style_inheritance_tested_patch.diff (13.7 kB) - added by voidlock on 09/15/06 07:30:28.
Updated code, now with tests
block-notation.diff (429 bytes) - added by voidlock on 07/11/07 02:46:20.
lets methods be defined inside a closure

Change History

06/22/06 07:38:26 changed by alex@twologic.com

  • attachment ruby_style_inheritance.diff added.

javascript OO ruby stle

09/03/06 20:43:16 changed by madrobby

  • status changed from new to closed.
  • resolution set to untested.

09/03/06 22:35:19 changed by anonymous

I tested this patch against all the tests that were bundled with subversion to make sure nothing broke in the library.

09/15/06 07:30:28 changed by voidlock

  • attachment ruby_style_inheritance_tested_patch.diff added.

Updated code, now with tests

09/15/06 07:42:23 changed by voidlock

  • status changed from closed to reopened.
  • resolution deleted.

The latest patch contains a few updates to the code. My code now exists in Class.extend, Class.mixin, and Class.inherit. From the original Prototype, Class.create just forwards calls to Class.extend. Likewise Object.extend uses Class.mixin to do the same job it did before. Any existing Prototype code will continue to work as expected.

I prefer the new syntax for reasons that can be found in my blog post about them: http://twologic.com/archives/2006/07/26/updated-oo-library/

Class.create and Object.extend could be deprecated or my methods could be rolled into the pre-existing ones. Any way you guys decide to do it, all the original prototype unit tests pass, along with the additional ones I've added to base.html, mixins.html and inherits.html. There is still probably a lot more code that could be covered by more unit tests, but I think I got all the basics in there.

06/18/07 23:07:09 changed by savetheclocktower

  • priority changed from normal to high.

Alex, we've used your inheritance implementation as the basis for the new inheritance branch. We're having internal discussions about syntax and naming, but I think the basic structure of the code will remain intact.

This ticket will now be used as a tracking ticket for inheritance support. We're aiming for 1.6 on this one.

07/11/07 02:46:20 changed by voidlock

  • attachment block-notation.diff added.

lets methods be defined inside a closure

07/11/07 03:53:33 changed by voidlock

The block-notation.diff patch should be applied against the current inheritance branch.

This patch lets a user optionally define a class inside a closure instead of a methods hash. Class definition inside a closure allows for some really interesting code, some of which I've outlined in a usage patterns pastie to save on comment length.

Usage Pastie URL: http://pastie.textmate.org/77884

08/04/07 04:03:54 changed by sam

  • status changed from reopened to closed.
  • resolution set to fixed.

(In [7264]) prototype: Merge -r6792:HEAD from ../branches/inheritance/{src/base.js,test/base.html}. Robust inheritance support for Class.create. Closes #5459.