Skip to content

Instantly share code, notes, and snippets.

View gnab's full-sized avatar

Ole Petter Bang gnab

  • Onyx CenterSource
  • Tønsberg, Norway
View GitHub Profile
# http://railstips.org/blog/archives/2009/08/07/patterns-are-not-scary-method-missing-proxy/
class BasicObject #:nodoc:
instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval/ }
end unless defined?(BasicObject)
class Proxy < BasicObject
def initialize(subject)
@subject = subject
end
class Seq
def self.unfold(*initial_state, &producer)
self.new(*initial_state, &producer)
end
def take_while(&condition)
current, state = @producer.call(@initial_state)
seq = [current]
while condition.call(current) do
resources: {
'messages' : {
'list' : function(req, resp) { ... } // GET til /messages
'get' : function(req, resp, id) { ... } // GET til /messages/id
'save' : function(req, resp, id) { ... } // POST til /messages
'update' : function(req, resp, id) { ...} // POST til /messages/is
}
}
@gnab
gnab / errorOnLineNumber.html
Created November 10, 2010 12:32
Get error line number of evaluated code in Firefox and Chrome
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
<body>
<script language="javascript">
var code = 'var a = c * 3;\nvar b = a + c;';
var script = $('<script />');
script.append('try { ' + code + ' } catch (e) { handleError(e); }');
@gnab
gnab / downscaledBoxShadow.html
Created December 1, 2010 20:44
Downscaled box shadow out of proportions
<!DOCTYPE html>
<html>
<head>
<style>
#box {
background: green;
height: 200px;
margin: 20px;
width: 200px;
-moz-transform: scale(2); /* OK */
@gnab
gnab / simple_grammar.rb
Created January 14, 2011 13:37
Parslet test
require 'parslet'
class SimpleExpressionParser < Parslet::Parser
rule(:space) { match['\s'].repeat(1) }
rule(:space?) { space.maybe }
rule(:integer) { match('\d').repeat(1).as(:int) >> space? }
rule(:identifier) { match('\w').repeat(1).as(:id) >> space? }
rule(:lparan) { str('(') >> space? }
final String relevantSkillz = "java, thrift, NoSQL, cassandra, hadoop, voip, TCPIP, SSO, ...";
if (relevantSkillz.contains(developer_skill)
&& developer_skill_level >= 31337
&& developer_wants_position_in == DEV_CORE_TEAM
&& developer_wants_to == KICK_ASS) {
final String improvementsToThisCode = System.in.readLine();
final String subject = "31337 - Elites wanted.";
mailto("[email protected]", subject, improvementsToThisCode);
@gnab
gnab / template.sh
Created May 29, 2011 20:38
Script for wrapping knockout.js template files in Javascript to be included in DOM
#
# Wraps the given files inside the following Javascript code for use with
# knockout.js:
#
# $('body').append("<script id=\"<templateId>\" type=\"text/html\"><templateContent></script>");
#
# - <templateId> the filename without the extension
# - <templateContent> the content of the file, with quotes escaped
#
for file in "$@"; do
@gnab
gnab / fizzbuzz.clj
Created June 21, 2011 12:48
FizzBuzz Kata in Clojure
(ns fizzbuzz (:use clojure.test))
(defn fizzbuzz [n]
(let [ret (str (when (= (rem n 3) 0) "Fizz")
(when (= (rem n 5) 0) "Buzz"))]
(if (empty? ret) n ret)))
(deftest fizzbuzz-test
(is (= (fizzbuzz 1) 1))
(is (= (fizzbuzz 2) 2))
@gnab
gnab / chop.clj
Created June 22, 2011 06:36
Karate Chop Kata in Clojure
(ns chop (:use clojure.test))
(defn chop [n nums]
(loop [nums nums offset 0]
(if (empty? nums)
-1
(let [i (int (/ (count nums) 2)) x (nth nums i)]
(cond
(= x n) (+ offset i)
(> x n) (recur (take i nums) offset)