News for hackers. Stuff that matters.
Re:Do you know Ruby’s ‘Chainsaw’ method? by warfangle on Thursday October 07, 2010 @03:39AM (#1767131)
While I know this is a rubyist thread, I want to chime in as a Javascript developer. You can almost do the same thing there - joy! Probably not the best way to do it, but it's 3AM and I'm writing off the top of my head. And it's a little bit messy. And it gets rid of that pesky emergency case.

Within the object constructor:

  // assuming the desk object was inherited and accessible. There are several ways to do this.
  var thisContext = this; // unless you want to do some weird stuff with calling a function that returns a function that calls a function
  ["flights", "local_transports", "hotels"].forEach(function(fn) {
    thisContext[fn] = function() {
      check_lunch_break && desk[fn](arguments);
    };
  });
  
Granted, this doesn't get your methods on the prototype, and will incur a cost every time you create one of these objects. If you properly inherited from the desk class, it will overwrite them. And the emergency function will work just fine, if defined on desk.
Re:Do you know Ruby’s ‘Chainsaw’ method? by troels on Thursday October 07, 2010 @03:53AM (#1767142)
Nitpickery indeed, but that won't work as expected. `fn` can be reused within the loop, depending on the runtime, so all methods will end up calling the same `desk[fn]`.
Re:Do you know Ruby’s ‘Chainsaw’ method? by warfangle on Thursday October 07, 2010 @11:22AM (#1768133)
You're right in that it won't work as expected - unless the desk methods are static. In reality, you would probably want to call

  desk[fn].call(thisContext, arguments);
Re:Do you know Ruby’s ‘Chainsaw’ method? by sharkbrainguy on Thursday October 07, 2010 @06:59AM (#1767438)
I think you might have misread the code.

Each `fn` is bound exactly once, `forEach` is not a loop.

Re:Do you know Ruby’s ‘Chainsaw’ method? by troels on Thursday October 07, 2010 @09:17AM (#1767686)
Touché