Skip to content

Instantly share code, notes, and snippets.

View ajlai's full-sized avatar

Anthony Lai ajlai

  • San Francisco Bay Area, CA
View GitHub Profile
@ajlai
ajlai / gist:1821466
Created February 13, 2012 23:34
.git_completions
# Working with Mac OS X 10.7, bash 1.3 (brew), git 1.7.4.4 (brew)
# To get this to work, add a line to source it from your ~/.bashrc
# Credit to Scott Bronson for the following tab completion workaround.
# https://github.com/bronson/dotfiles/blob/731bfd951be68f395247982ba1fb745fbed2455c/.bashrc#L81
# (only for bash, zsh tab completions are done separately.)
__define_git_completion () {
eval "
_git_$2_shortcut () {
COMP_LINE=\"git $2\${COMP_LINE#$1}\"
let COMP_POINT+=$((4+${#2}-${#1}))
@ajlai
ajlai / yaml_nav.rb
Created April 2, 2012 22:27
YamlNav
require 'psych'
require 'pathname'
# Hash-like access to objects stored in yaml files in directory structures.
#
# Examples:
# CONFIGS = YamlNav.new(Rails.root + "config")
#
# CONFIGS["exceptional.yml"]["api-key"]
# # => the api-key stored in config/exceptional.yml
@ajlai
ajlai / self_unless.rb
Created December 13, 2012 00:14 — forked from anonymous/self_unless.rb
Making Ruby || / && chaining for non nil/false values nicer
class Object
# Usage:
# maybe_zero.self_unless(&:zero?) || zero_fallback
# # => either maybe_zero or zero_fallback (if maybe_zero was zero)
def self_unless
self unless yield(self)
end
# Usage:
# maybe_present.self_if(&:present?)
# Facilitate manipulating symbols as AR attribute values while storing them as other values in the DB
#
# Examples
#
# class Example < ActiveRecord::Base
# serialize :type, SymbolMapper.for(foo: 1, bar: 2, baz: 3)
# end
#
# example.type = :foo
# # => :foo
@ajlai
ajlai / async_file_upload.dart
Created July 19, 2013 18:44
Async File Uploading in Dart
void handleUpload() {
var elem = query("#upload") as FileUploadInputElement;
var file = elem.files.first;
FormData fd = new FormData(null);
fd.append("username", "ajlai");
fd.appendBlob("Filename", file);
var req = new HttpRequest();
req.open("POST", "/foo");
@ajlai
ajlai / coalesce.dart
Last active December 20, 2015 00:39
null-coalescing and input checking in dart
library coalesce;
// To turn the situation where we have:
// var foo;
// if (mightBeNullA != null) {
// foo = mightBeNullA;
// } else if (mightBeNullB != null) {
// foo = mightBeNullB;
// } else {
// foo = 2;
@ajlai
ajlai / diff difference
Last active December 20, 2015 08:49
Size comparison beween `diff` and `diff -U 0`
Size comparison beween `diff` and `diff -U 0`
@ajlai
ajlai / 0-rounds_to.rb
Last active December 21, 2015 07:48
Ruby rounds_to? Allows for a rough equality check to an implicit precision provided by the input `numeric` argument
class Numeric
# Returns true if for any i, self.round(i) == numeric
#
# Usage:
#
# 149.835.rounds_to?(0) #=> true
# 149.835.rounds_to?(100) #=> true
# 149.835.rounds_to?(150) #=> true
# 149.835.rounds_to?(149.8) #=> true
# 149.835.rounds_to?(149.84) #=> true
@ajlai
ajlai / date_series_redshift.sql
Created February 9, 2015 20:09
Date series on redshift
WITH date_series_bounds AS (
SELECT date('2012-12-21') as start, date('2013-08-23') as end
), date_series AS (
select date(days.start + days.interval)
from (
select bounds.start, generate_series(0, bounds.end - bounds.start) AS interval from date_series_bounds bounds
) as days
)
select * from date_series
-- 2012-12-21