Skip to content

Instantly share code, notes, and snippets.

@CootCraig
CootCraig / webapp.rb
Created November 17, 2010 23:34 — forked from igrigorik/webapp.rb
require 'rubygems'
require 'rack'
class Object
def webapp
class << self
define_method :call do |env|
func, *attrs = env['PATH_INFO'].split('/').reject(&:empty?)
[200, {}, send(func, *attrs)]
end
@CootCraig
CootCraig / ancestors.rb
Created December 4, 2010 22:23
Show the ancestor classes of an object
module TryItOut
class << self
def ancestors(o)
a = []
a << ((o.class == Class) ? o : o.class)
loop {
break if a[-1].nil?
a << a[-1].superclass
}
a
@CootCraig
CootCraig / proc_demo.rb
Created December 31, 2010 17:40
Demo. Proc.new closure. Using call and method block.
# Output
# p1_a.call(1) => proc1 - a - 1
# p1_b.call(2) => proc1 - b - 2
# p2_o.call(3) => proc2 - o - 3
# p2_p.call(4) => proc2 - p - 4
# TestClass.m_block(&p1_a) => method TestClass.m_block yield(100) => proc1 - a - 100
# TestClass.m_block(&p1_b) => method TestClass.m_block yield(101) => proc1 - b - 101
# TestClass.m_block(&p2_o) => method TestClass.m_block yield(102) => proc2 - o - 102
# TestClass.m_block(&p2_p) => method TestClass.m_block yield(103) => proc2 - p - 103
@CootCraig
CootCraig / store_page_on_session
Created January 22, 2011 17:38
storing page on session for will_paginate
class ApplicationController < ActionController::Base
...
before_filter :page_params, :only => :index
def page_key
(self.class.to_s + "_page").to_sym
end
# Use a before_filter on index action to remember the current page
@CootCraig
CootCraig / first success
Created February 2, 2011 19:19
Rails nonmodel select
= select_tag('tweet_source', options_for_select([['',0],['My Tweets',1],['Retweets',2],['Favorites',3],['Mentions',4]],2), {})
<select name="tweet_source" id="tweet_source"><option value="0"></option>
<option value="1">My Tweets</option>
<option selected="selected" value="2">Retweets</option>
<option value="3">Favorites</option>
<option value="4">Mentions</option></select>
Want the onchange event to GET on tweets controller index action with tweet_source in params.
@CootCraig
CootCraig / Log wget
Created February 12, 2011 19:56
Read Life cell file
➜ crockford_js_the_good_parts git:(master) ✗ wget -d http://www.conwaylife.com/pattern.asp?p=bargespaceship_106.lif
wget -d http://www.conwaylife.com/pattern.asp\?p\=bargespaceship_106.lif
DEBUG output created by Wget 1.12 on linux-gnu.
--2011-02-12 12:49:20-- http://www.conwaylife.com/pattern.asp?p=bargespaceship_106.lif
Resolving www.conwaylife.com... 65.182.101.250
Caching www.conwaylife.com => 65.182.101.250
Connecting to www.conwaylife.com|65.182.101.250|:80... connected.
Created socket 3.
Releasing 0x08aba240 (new refcount 1).
@CootCraig
CootCraig / neighborhood.js
Created March 1, 2011 18:05
JS prototypal object creation 3/1/2011
// Working on my JS idioms
// fragment, not tried yet.
neighborhood = new function() {
// prototype object saved in closure
var proto = {
add_cell: function(location) {
var key = location.key();
if (this.cells_and_neighbors.hasOwnProperty(key)) {
this.cells_and_neighbors[key].revive();
} else {
@CootCraig
CootCraig / index.html
Created March 3, 2011 19:17
Morphing JavaScript object behavior
<!DOCTYPE html>
<html>
<head>
<title class="page_title">Page Title Here</title>
<link rel="stylesheet" type="text/css" href="base.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
</head>
<body>
<h1 class = "page_title">Page Title Here</h1>
<h3>Morph an objects behavior by switching function definitions.</h3>
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
//
if(!window.jq && window.jQuery){
var jq = jQuery;
};
if(!window.App){
window.App = {};
@CootCraig
CootCraig / prawn_ttf_and_builtin.rb
Created April 29, 2011 03:40
Prawn TTF font versus Built-in font
require 'prawn'
Prawn::Document.generate "my.pdf" do
font_size 40
font "Times-Roman"