Skip to content

Instantly share code, notes, and snippets.

# This is more "Ruby", and I agree that I don't necessarily agree with
# all the syntax choices. The else condition isn't required as the
# function returns nil if no other code paths executed. You could argue
# that, that is a regression waiting to happen though.
def just_one_kind_of_response? item, controller
case controller
when 'actions'
item.comments.blank? or item.suggestions.blank? or detail.comments.blank?
when 'thoughts'
@mxcl
mxcl / fswatcher.c
Created November 27, 2009 10:38
C code to watch a directory for changes
// gcc -o fswatcher -framework Carbon fswatcher.c
#include <CoreServices/CoreServices.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/dir.h>
static time_t timestamp = 0;
static void callback(ConstFSEventStreamRef stream,
@mxcl
mxcl / tell
Created February 4, 2010 13:40
Convenience script for running certain Applescripts
#!/usr/bin/ruby
#
# Example usage:
# tell iTunes to pause
# tell iTunes to play
# tell Linkinus to quit
#
# http://twitter.com/mxcl
# we add it back later
@mxcl
mxcl / fixnum+base62.rb
Created February 6, 2010 16:17
Readable Base 62 encoder
# Use this for your URL shortening needs
# Base 62 is as good as you can easily get for URL shortening
# And it's effective: 1_000_000 => '4c92'
# Challenge: fork and speed up the implementation!
class Fixnum
TOKENS = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
# NOTE returns nil for 0
def to_62_s
@mxcl
mxcl / HeinleinQuote.md
Created February 24, 2010 17:34
Robert A. Heinlein Quote

“A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.”

I think it's from “The moon is a harsh mistress”

@mxcl
mxcl / gist:360525
Created April 8, 2010 20:52
Make your Xcode templates less rubbish
def inreplace path, before=nil, after=nil
[*path].each do |path|
f = File.open path, 'r'
s = f.read.gsub before, after
f.reopen(path, 'w').write s
f.close
end
end
require 'pathname'
@mxcl
mxcl / tdb
Created June 22, 2010 10:25
Makes ADB logcat more readible (for me)
#!/usr/bin/ruby
RED = "\033[0;31m"
RESET = "\033[0m"
YELLOW = "\033[0;33m"
BLUE = "\033[0;35m"
GREY = "\033[0;34m"
trap("INT") { puts; exit! 127 }
@mxcl
mxcl / NSURLRequest+mxcl.m
Created October 22, 2010 15:35
NSURLRequest+mxcl.m
#import <Foundation/Foundation.h>
@interface NSURLRequest (mxcl)
+ (id)requestWithURL:(NSURL *)url;
+ (id)requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)cachePolicy timeoutInterval:(NSTimeInterval)timeoutInterval;
+ (id)requestWithURLString:(NSString *)url; // for convenience
@end
@mxcl
mxcl / fizzbuzz.rb
Created November 27, 2010 11:53
Improve this Fizzbuzz!
#!/usr/bin/ruby
# The Ruby FizzBuzz with the best concision versus elegance compromise.
# Fork if you have better ideas.
1.upto 100 do |n|
puts [[:fizz][n%3], [:buzz][n%5]].join[/.+/m] || n
end
@mxcl
mxcl / brew-foo.rb
Created August 4, 2012 21:24
Homebrew environment in new Ruby script
$:.unshift(`brew --prefix`.chomp+'/Library/Homebrew')
require 'global'
Formula.each do |f|
p f
end