Object Like Hash

4.28.2013

"My crime is that of curiosity"

While I was going through the source code of camping this piece of code that gives you Object like Hash got my attention. Instead of using Hash['key'] it allows you to use Hash.key hence the name "Object Like Hash".

Code

class H < Hash
    def method_missing(m,*a)
          m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super
      end

      undef id, type if ?? == 63
  end

Usage

read more

Node EventEmitter

4.21.2013

"Talk is cheap show me the code"

var EventEmitter = require('events').EventEmitter;

var Ticker = function(){
    var self = this;
    EventEmitter.call(self);
    self.startTicking = function(){
        self.emit('tick');
        setTimeout(self.startTicking, 1000);
    }
}

Ticker.prototype = new EventEmitter();

var ticker = new Ticker();

ticker.startTicking();

ticker.on('tick', function(){
    console.log('got a tick');
});

read more