Skip to content

Instantly share code, notes, and snippets.

View skanev's full-sized avatar

Stefan Kanev skanev

View GitHub Profile
@skanev
skanev / gist:5244311
Created March 26, 2013 10:08
Best Commit Ever
commit 79985f579cbe84722fde40a27271d513c2083d4a
Author: Stefan Kanev <[email protected]>
Date: Tue Mar 26 12:06:58 2013 +0200
Update current year
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 2cb2c01..e48b724 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
(ns lectures.parser-test
(:use clojure.test
lectures.parser)
(:require [clojure.string :as str]))
(defmacro expect
[parser & body]
`(are [input# output#] (= (parse ~parser input#) output#)
~@body))
@skanev
skanev / digraphs.vim
Created January 23, 2013 10:16
Some custom digraphs I use often.
let s:digraphs = [
\ ['eo', 'element of', '∈'],
\
\ ['^0', 'to the zeroeth power', '⁰'],
\ ['^1', 'to the first power', '¹'],
\ ['^2', 'to the second power', '²'],
\ ['^3', 'to the third power', '³'],
\ ['^4', 'to the fourth power', '⁴'],
\ ['^5', 'to the fifth power', '⁵'],
\ ['^6', 'to the sixth power', '⁶'],
@skanev
skanev / generic_operations.py
Created November 8, 2012 08:48
Data-Directed Programming Example Code
operations = {}
def get(name, types):
return operations.get((name,) + types)
def put(name, args, code):
operations[(name,) + args] = code
def apply_generic(name, *args):
types = tuple(map(type, args))
@skanev
skanev / shadowing.js
Created October 31, 2012 21:53
Scoping and shadowing in JavaScript
var x = 1;
(function() {
var x = x + 1; // prints NaN
console.log(x);
})();
(function (x) {
console.log(x); // prints 2
})(x + 1);
@skanev
skanev / ABOUT.markdown
Created October 2, 2012 12:22
Weird bug

I've inherited a legacy Rails app that runs on Ruby 1.8.6. I want to upgrade to 1.8.7, but I get weird bugs. One of them was a weird CSS glitch. Here's what happens:

Note how input.html.erb renders <div id="ribbon"> and then a partial. If you look at the output, the partial gets rendered in both versions of Ruby, but the <div id="#ribbon"> gets rendered only in 1.8.6. It appears that the culprit is the small fragment containing a ruby comment <% #end top div %>. If I remove it, both work the same.

Is this an ERB issue and why does switching Ruby versions trigger it?

@skanev
skanev / set_attributes.py
Created August 24, 2012 11:45
Set arguments passed to the constructor as attributes
import inspect
from functools import wraps
def set_attributes(constructor):
@wraps(constructor)
def wrapped(self, *args, **kwargs):
names = inspect.getargspec(constructor).args
for (key, value) in dict(zip(names[1:], args)).items():
setattr(self, key, value)
for (key, value) in kwargs.items():
@skanev
skanev / points_breakdown.sql
Created July 20, 2012 17:09
Why do it with Ruby, when 63 lines of SQL would also work :)
CREATE VIEW points_breakdowns AS
WITH users_tasks AS (
SELECT
users.id AS user_id,
tasks.id AS task_id,
GREATEST(COALESCE(points + adjustment, 0), 0) AS points
FROM users
INNER JOIN tasks ON TRUE
LEFT JOIN solutions ON (solutions.task_id = tasks.id AND solutions.user_id = users.id)
ORDER BY users.id, tasks.id
@skanev
skanev / extract_variable.vim
Created May 4, 2012 07:33
Extract a variable in vim script
" Select an expression, hit <Leader>e and type the variable name
function! ExtractVariable()
try
let save_a = @a
let variable = input('Variable name: ')
normal! gv"ay
execute "normal! gvc" . variable
execute "normal! O" . variable . " = " . @a
finally
@skanev
skanev / githelpers.sh
Created April 28, 2012 20:17
Improved pretty git log
#!/bin/bash
# An improved (although heavily obfuscated) version of @garybernhardt's
# git pretty log, show in Destroy All Software №63. Said improvements are:
#
# * Minimize the whitespace between the columns - use a single space instead
# of whatever the color codes require
# * Colorize the graph, as with git log --graph
# * Slightly simplify the sed regular expression
#