Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

« Newer Snippets
Older Snippets »
2 total  XML / RSS feed 

Never render the full layout in a Rails view when called via AJAX

Essential for degradable javascript, when one never knows if a view or partial is being called via XmlHttpRequest or not. Throw the below code in your ApplicationController (application.rb)

More info in this blog post

def render(*args)
        args.first[:layout] = false if request.xhr? and args.first[:layout].nil?
        super
end

Meta programming in D

Implementing guards in D

import std.stdio;
bool guarded( lazy void cmd, bool delegate()[] guards ...){
  bool lazyOr= false;
  foreach( g; guards) {
    if( lazyOr |= g()) break;
  }
  bool yn= !lazyOr;
  if( yn) cmd();
  return yn;
}
bool guard( lazy bool exp, lazy void action){
  bool yn= exp;
  if( yn) action();
  return yn;
}
void main(){
  int i=2;
  guarded( {
      writefln("yes");
      writefln("must i");
    }(),
      guard( i==0, writefln("null")),
      guard( i==1, writefln("one"))
  );
}
« Newer Snippets
Older Snippets »
2 total  XML / RSS feed