Skip to content

Instantly share code, notes, and snippets.

@alloy-d
alloy-d / craigslist.org.js
Created March 13, 2012 23:05
.js script to check ghosting status of a Craigslist post
var MONTH_NAMES = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
@alloy-d
alloy-d / alcohol.rb
Created May 19, 2012 03:17
'cause robots should type drunk, too
class Array; def random; self[rand(self.count)]; end; end
class Hash; def random; self[self.keys.random]; end; end
class Alcohol
DRINKING_START = 20
PEAK_INTOXICATION = 4
LAYOUT = [
['`1234567890-= ', '~!@#$%^&*()_+ '],
[' qwertyuiop[]\\', ' QWERTYUIOP{}|'],
@alloy-d
alloy-d / Default.sublime-keymap
Created June 11, 2012 16:36
Sublime Text 2 Vintage mode keybindings for Colemak based on my Colemak setup for vim
[
{ "keys": ["escape"], "command": "exit_insert_mode",
"context":
[
{ "key": "setting.command_mode", "operand": false },
{ "key": "setting.is_widget", "operand": false }
]
},
{ "keys": ["escape"], "command": "exit_visual_mode",
@alloy-d
alloy-d / sublime-issues.markdown
Created June 11, 2012 16:53
Issues I have with Sublime Text 2 that I want to solve.

Autopairing

  • Automatic insertion of {} when typing # in strings in Ruby (not in CoffeeScript). This would be fine if those soaked up the next { that is typed, as happens with quotes.
    • Ideal behavior here is treating {} like quotes—only insert the pair when the first is typed.
  • Occasional misinterpretation of typed quotes—they get soaked up into existing quotes instead of being inserted as new quotes.

Spacing

  • Extra spacing is not stripped from blank lines.
  • Fixed by setting trim_trailing_white_space_on_save to true.
@alloy-d
alloy-d / Default.sublime-commands
Created July 15, 2012 21:53
Simple Sublime Text 2 plugin to enable quick toggling of the 'draw_white_space' setting between 'selection' and 'all'.
[
{"caption": "Toggle Shown Spaces", "command": "toggle_shown_spaces"}
]
@alloy-d
alloy-d / callback-example.js
Created September 8, 2012 22:17
Ridiculous callback example
// We're coding an awesome website.
//
// For a great user experience, we want to add a personalized greeting.
// Here is a function that gives said personalized greeting, given the user's name.
function greetUserLikeIts1995(firstName) {
alert("Hello there, " + firstName + "!");
}
// For security, we use carrier pigeons to get information from our users.
// Unfortunately, it's a slow process, so we don't want to just sit around
@alloy-d
alloy-d / beginning-of-day.js
Created September 19, 2012 17:38
Given a JS date object, get an ISO 8601 string for the beginning of that day in a target timezone.
var beginningOfDayWithOffset = function (targetOffset) {
var pad = function (n) { return (n.toString().length === 2 ? n.toString() : "0" + n) }
var sign = "-";
var offsetHours, offsetMinutes, offsetString;
// That's right, JavaScript's timezone offset has the opposite sign of the
// offset in ISO 8601...
if (targetOffset <= 0) {
sign = "+";
@alloy-d
alloy-d / questions.json
Created October 2, 2012 17:03
JSON representing comment fields for a blog on which identity is a name and a spirit animal.
[
{
"name": "commenter[name]", // the name attribute; should also be used to create the id
"text": "Your name (pseudonyms are okay)", // the text of an associated label
"required": true, // this field should not be left blank
}, // => A basic text field
{
"name": "commenter[email]",
"text": "Your email address (will not be shown)",
"type": "email", // this field represents an email address
@alloy-d
alloy-d / options_with_indifferent_access.rb
Created October 25, 2012 23:14
Awful Ruby hack for forcing the last argument (generally an options hash) to all of a class's methods to be a HashWithIndifferentAccess.
module OptionsWithIndifferentAccess
module ClassMethods
def method_added(method_name)
unless caller[1].match(/`method_added'/)
define_method("_orig_#{method_name}".intern, instance_method(method_name))
define_method(method_name) do |*args, &block|
if args[-1].is_a? Hash
args[-1] = HashWithIndifferentAccess.new(args[-1])
end
self.send "_orig_#{method_name}", *args, &block
@alloy-d
alloy-d / awareness-utils.rb
Created December 3, 2012 19:23
Fun with Ruby Procs and the stack.
module AwarenessUtils
# Example: `define_method :current_method_name, StackWalker.new(2)`
class StackWalker < Proc
def self.new(depth=1)
super() { instance_eval { caller[depth] =~ /`(.+)'/ and $1 } }
end
end
end