Skip to content

Instantly share code, notes, and snippets.

View kastiglione's full-sized avatar

Dave Lee kastiglione

View GitHub Profile
@kastiglione
kastiglione / gist:1062860
Created July 4, 2011 03:14
Hostname banning zsh function
# assumes control of all hosts in /Local/Default/Hosts
# https://gist.github.com/1062860
hostoggle() {
if (( ${#*} == 0 )); then
# no host given, print current list of hosts banned
dscl . -ls Hosts
fi
for host in "$@"; do
if $(dscl . -read Hosts/$host >&- 2>&-); then
# delete existing host records (raw and www-less)
@kastiglione
kastiglione / tldre.rb
Created April 20, 2012 03:57
Generate a TLD regex
require 'open-uri'
# monkey patching, but we're still cool right?
class Fixnum
def to_proc
->(obj) { obj[self] }
end
end
class Hash
@kastiglione
kastiglione / rubymotion-app-setup-after-hook.rb
Created July 28, 2012 22:14
RubyMotion App::setup after hook
class << Motion::Project::App
real_setup = instance_method(:setup)
define_method :setup do |&block|
real_setup.bind(self).call(&block)
# run after hook here
end
end
@kastiglione
kastiglione / custom_store.rb
Created October 4, 2012 21:04
Setup a minimal NSIncrementalStore subclass for learning / experimenting with MacRuby
framework 'CoreData'
# Create a minimal model
$thing = NSEntityDescription.alloc.init
$thing.name = 'Thing'
$model = NSManagedObjectModel.alloc.init
$model.entities = [$thing]
# Create the custom store class
class CustomStore < NSIncrementalStore
class AppDelegate
def application(application, didFinishLaunchingWithOptions:launchOptions)
a = NSEntityDescription.new
a.name = 'A'
b = NSEntityDescription.new
b.name = 'B'
bs = NSRelationshipDescription.new
bs.name = 'bs'
bs.destinationEntity = b
bs.ordered = true
CGRect CGRectIntegralScaledEx(CGRect rect, CGFloat scale)
{
return CGRectMake(floorf(rect.origin.x * scale) / scale, floorf(rect.origin.y * scale) / scale, ceilf(rect.size.width * scale) / scale, ceilf(rect.size.height * scale) / scale);
}
CGRect CGRectIntegralScaled(CGRect rect)
{
return CGRectIntegralScaledEx(rect, [[UIScreen mainScreen] scale]);
}
@kastiglione
kastiglione / dynamic_rescue.rb
Last active December 14, 2015 12:08
Rescue on more than just exception type.
module UncaughtThrowError
def self.===(ex)
ArgumentError === ex && ex.message =~ /uncaught throw/
end
end
begin
throw :foo
rescue UncaughtThrowError
puts 'Success: Rescued UncaughtThrowError'
@kastiglione
kastiglione / gist:5475944
Last active December 16, 2015 18:09
Compose a signal corresponding to whether a source signal has sent an equal number of YESes and NOs.
[[[signal
// Map YES to 1, NO to -1
map:^(NSNumber* b) {
return @(b.boolValue ? 1 : -1);
}]
// Running sum of yes/1's and no/-1's
startWithScan:@0 combine:^(NSNumber* running, NSNumber* next) {
return @(running.intValue + next.intValue);
}]
// A 0-sum means equal number of YES/NO's; Map 0 to YES
#!/usr/bin/env casperjs
#
# Example: ./github-users.coffee Greenville
#
{Casper} = require 'casper'
class GitHubUserScraper extends Casper
USER_AGENT: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11'
@kastiglione
kastiglione / gist:5679834
Last active December 17, 2015 22:09 — forked from alloy/gist:5679340
- (NSArray *)mapConcurrent;
{
NSArray *originals = self.listOfObjects;
NSUInteger count = originals.count;
NSMutableArray *collected = [[NSMutableArray alloc] initWithCapacity:count];
for (NSUInteger i = 0; i < count; i++) {
[collected addObject:[NSNull null]];
}