Skip to content

Instantly share code, notes, and snippets.

View jeffreyiacono's full-sized avatar

Jeff Iacono jeffreyiacono

  • Sierra.ai
  • San Francisco
  • X @jfi
View GitHub Profile
@jeffreyiacono
jeffreyiacono / eigenclasses-alt.rb
Created November 7, 2012 06:34
Ruby eigenclass example
# alternatively
class Dog
def bark
puts "bark bark"
end
end
fido = Dog.new
rex = Dog.new
@jeffreyiacono
jeffreyiacono / short.sql
Created November 7, 2012 01:52
short sql function (an example)
DROP FUNCTION IF EXISTS shorten;
DELIMITER $$
CREATE FUNCTION shorten(s VARCHAR(255), n INT) RETURNS VARCHAR(255)
BEGIN
IF ISNULL(s) THEN
RETURN '';
ELSEIF n < 15 THEN
RETURN LEFT(s, n);
@jeffreyiacono
jeffreyiacono / url-parser.js
Created November 7, 2012 00:29
url parsing in js
// tested in chrome
link = document.createElement("a")
// <a></a>
link.setAttribute("href", "https://whatever.com:3000/some-page?foo=bar#baz")
// undefined
link.href
// https://whatever.com:3000/some-page?foo=bar#baz
link.protocol
// https:
link.host
@jeffreyiacono
jeffreyiacono / db.sh
Created October 23, 2012 06:51
db.sh
#!/bin/sh
# via: http://capotej.com/blog/2012/10/07/an-embedded-key-value-store-for-shell-scripts/
DBFILE=example.db
put(){
echo "export kv_$1=$2" >> $DBFILE
}
@jeffreyiacono
jeffreyiacono / avg.rb
Created October 19, 2012 18:06
average
module Enumerable
def avg
reduce(0.0, :+) / length
end
end
@jeffreyiacono
jeffreyiacono / column-fun.sh
Created October 18, 2012 01:11
fun with columns
$ cat test.csv
cat,dog,mouse
up,down,left,right
red,green,blue
$ cat test.csv | column -t -s,
cat dog mouse
up down left right
red green blue
@jeffreyiacono
jeffreyiacono / flocker.sh
Created October 15, 2012 05:06
Flocker
#! /bin/sh
die() {
echo $1
exit 1
}
(
flock -n 9 || die "couldn't acquire ze lock"
echo "starting ..."
sleep 10
@jeffreyiacono
jeffreyiacono / vim-windowing.md
Created October 1, 2012 06:19
(m/g)vim windowing

(M/G)VIM

Open N windows split horizontally / vertically

-o[N]                Open N windows (default: one for each file)
-O[N]                Like -o but split vertically

you can also specify files:

vim -o3 file1.txt file2.txt file3.txt
@jeffreyiacono
jeffreyiacono / date-to-datetime-comparison.sql
Created September 11, 2012 00:27
comparison of date to datetimes in MySQL
-- DATETIME to DATE comparison
SELECT CAST('2012-09-09 23:59:59' AS DATETIME) <= DATE('2012-09-10 00:00:00'); -- TRUE :)
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) <= DATE('2012-09-10 00:00:00'); -- TRUE :)
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) <= DATE('2012-09-10 23:59:59'); -- TRUE :)
SELECT CAST('2012-09-10 00:00:01' AS DATETIME) <= DATE('2012-09-10 23:59:59'); -- FALSE :(
-- and a little more proof:
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) = DATE('2012-09-10 00:00:00'); -- TRUE
SELECT CAST('2012-09-10 00:00:00' AS DATETIME) = DATE('2012-09-10 00:00:01'); -- TRUE
SELECT CAST('2012-09-10 23:59:59' AS DATETIME) = DATE('2012-09-10 23:59:59'); -- FALSE
@jeffreyiacono
jeffreyiacono / cmds
Created September 6, 2012 02:29
fun with unix programs
$ cat sample | sort | uniq -c
2 bat
4 cat
2 dog
1 gator
$ cat sample | sort | uniq -c | sed "s/^[ \t]*//"
2 bat
4 cat
2 dog