Skip to content

Instantly share code, notes, and snippets.

@agoodman
agoodman / SetIntersection.m
Created October 20, 2010 00:36
Cocoa Set Intersection
NSArray* set1 = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
NSArray* set2 = [NSArray arrayWithObjects:@"two",@"three",@"four",nil];
NSMutableArray* set3 = [NSMutableArray arrayWithCapacity:MIN(set1.count,set2.count)];
for (NSString* element in set1) {
if( [set2 containsObject:element] ) {
[set3 addObject:element];
}
}
@agoodman
agoodman / gist:821337
Created February 10, 2011 21:03
GCD Thread Redirecting
/* say you have a process driven by the main thread, such as
* a UI control, and you want the process to execute on a
* background thread, but once it's done, you want to update
* some UI element. that must happen on the main thread, so
* here's an example of how you might do that
*/
- (IBAction)buttonClicked
{
dispatch_async(backgroundProcessingQueue, ^{
@agoodman
agoodman / id10t.c
Created May 9, 2011 02:02
Real world example of "new guy" fail
// if you don't know why this doesn't compile, find another job
int tBrains = 9;
if( tBrains<10 ) {
int tZombies = 1;
}
tZombies += tBrains;
@agoodman
agoodman / user_export.rb
Created June 2, 2011 13:57
User export to CampaignMonitor example
CSV.open("users.csv","wb") {|csv| User.all.each {|user| csv << [user.email, "#{user.first_name} #{user.last_name}"]}}
@agoodman
agoodman / prefix.pch
Created July 18, 2011 17:10
Useful abbreviations
// GCD
#define async_main(aBlock) dispatch_async(dispatch_get_main_queue(), aBlock)
#define async_global(aBlock) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), aBlock)
// UIKit
#define Alert(aTitle,aMessage) [[[[UIAlertView alloc] initWithTitle:aTitle message:aMessage delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK",nil] autorelease] show]
#define NetworkAlert Alert(@"Network error",@"Please check your network connection.")
@agoodman
agoodman / filtered_scope.rb
Created August 5, 2011 20:40
Ruby on Rails scope filtering
# Model:
#
# class Example < ActiveRecord::Base
# end
#
# Controller:
#
# class ExamplesController < ActionController::Base
# def index
# @examples = Example.filtered_scope(params[:filter])
@agoodman
agoodman / monthly_activity.rb
Created August 25, 2011 16:44
Determinging monthly activity
# this is really useful for collecting record counts into discrete buckets for visualization
(1..6).collect {|n| Query.where(['created_at > ?',Date.today-n.months]).where(['created_at < ?',Date.today-(n-1).months]).count}
@agoodman
agoodman / gist:1953447
Created March 1, 2012 21:45
Nested picker.assignments.member
<picker>
<code>00000001</code>
<created-at type="datetime">2012-02-24T19:57:17Z</created-at>
<dob type="date" nil="true"></dob>
<first-name>andy</first-name>
<id type="integer">12</id>
<last-name>smith</last-name>
<mail-address nil="true"></mail-address>
<mail-city nil="true"></mail-city>
<mail-state nil="true"></mail-state>
@agoodman
agoodman / subtract_letters.rb
Created May 24, 2012 22:42
Word Pruning by Letter Subtraction
possible_words = ["seek", "find", "ignore", "pursue", "covet"]
available_letters = ["a", "c", "e", "f", "g", "i", "n", "o", "p", "r", "t", "u", "v"]
words = Array[possible_words].flatten
available_letters.each {|e| words = words.map {|w| w.sub(e, '')}}
viable_words = words.each_with_index.collect {|w,k| possible_words[k] if w==""}.compact
@agoodman
agoodman / bad_ssl_controller.rb
Created January 15, 2013 04:15
bad ssl practice in controller logic
class BadSslController < ApplicationController
before_filter :enforce_https
def enforce_https
if use_ssl?
if request.protocol != 'https://'
redirect_to url_for(controller: controller_name, action: action_name, protocol: 'https') and return
end
else