Skip to content

Instantly share code, notes, and snippets.

@domgetter
domgetter / havewant.rb
Last active August 3, 2016 20:19
Will tell you what Ruby method(s) you want given the input and output Now works with arguments!
class HaveWant
attr_reader :answers
def initialize(*have, want)
@have = have[0]
@args = have[1..-1]
@want = want
find_matches
# config/routes.rb
resources :documents do
scope module: 'documents' do
resources :versions do
post :restore, on: :member
end
resource :lock
end
end
@javan
javan / application_controller.rb
Created November 30, 2013 22:06
Prevent cross-origin js requests
class ApplicationController < ActionController::Base
before_filter :ensure_xhr
private
def ensure_xhr
if request.get? && request.format && (request.format.js? || request.format.json?)
head :forbidden unless request.xhr?
end
end
end
@sj26
sj26 / journey_coffee_generator.rb
Created April 10, 2013 03:53
Generate rails-like route helpers for use in javascript land
class JourneyCoffeeGenerator < Journey::Visitors::Visitor
def accept node
@requirements = [[]]
super.gsub('" + "', '')
end
private
def visit_GROUP node
@requirements << []
@simonmcmanus
simonmcmanus / frax.js
Last active December 15, 2015 10:29
PJAX as Page.js middleware.
var frax = function(context, next) {
if(!context.init) {
$.get(context.canonicalPath+'?_pjax=true', function(markup) {
if(context.delay) {
context.pending = markup;
}else {
context.container.html(markup); // cached $('#container')
}
next();
});
@rwjblue
rwjblue / readme.md
Last active May 13, 2016 18:10
Guide to using drip with JRuby

#Overview drip is an awesome command line tool that can be used to dramatically lower perceived JVM startup time. It does this by preloading an entirely new JVM process\instance and allowing you to simply use the preloaded environment. This has extraordinary results with jruby.

We reduced time to run rake environment from 13 seconds to a mere 3.5 seconds. This is actually at or near MRI 1.9.3p327 (with falcon patch) speeds!

Adding a few addition jruby options will reduce startup time even further (down to 1.69 seconds).

#Install Drip Install drip if you haven't already (see https://github.com/flatland/drip)

@shirosaki
shirosaki / 0-recipe.md
Created December 4, 2012 01:30
Build 1.9.3-p327 with falcon patch on Windows
# clone latest rubyinstaller from github
$ git clone https://github.com/oneclick/rubyinstaller.git
$ cd rubyinstaller

# download patches into resources\patches\ruby193 directory
$ mkdir resources\patches\ruby193
$ cd resources\patches\ruby193
$ curl -o 1-falcon.patch https://raw.github.com/gist/4136373/falcon.diff
$ curl -O https://raw.github.com/gist/4199712/2-fix-file-expand_path-globing.patch
@ryanb
ryanb / issues_with_modules.md
Created November 29, 2012 22:38
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
@mattbaker
mattbaker / object.prototype.js
Created June 14, 2011 18:54
Object.tap in Javascript
Object.prototype.tap = function(f){f.apply(this); return this;}
var x = {a:2};
x.tap(function(){console.log(this.a)}).a = 4; //Prints 2
console.assert(x.a == 4);
# Extend jQuery objects with Underscore collection methods.
#
# Each collection method comes in two flavors: one prefixed
# with _, which yields a bare DOM element, and one prefixed
# with $, which yields a jQuery-wrapped element.
#
# So if `this` is a jQuery object, instead of:
#
# _.max @, (el) -> $(el).height()
#