Skip to content

Instantly share code, notes, and snippets.

@pnc
pnc / ruby_block.rb
Created June 28, 2011 15:50
Ruby blocks and required parentheses
def group(symbol, &block)
block.call
end
symbol = :cheese
# Simple argument-and-block form:
group symbol do
puts "hello"
end # => "hello"
@pnc
pnc / deprecation.rb
Created July 26, 2011 01:18
Moar deprecation warnings
set_trace_func(Proc.new do |event, file, line, id, binding, classname|
if rand > 0.999
puts "warning: #{classname} is deprecated and will be removed in a future release"
puts " (called from: #{file}:#{line})"
end
end)
@pnc
pnc / troll.js.coffee
Created August 4, 2011 14:00
This is what happens when you work with Emacs users.
(for own key, value of assignable_attributes
attribute_scope = $('#reservation_' + key)
attribute_scope.val(value)
attribute_scope.change()
)
(for own key, value of ui.item.attribute_labels
$(".reservation-label-#{key}").html(value)
)
@pnc
pnc / AppDelegate.m
Created January 10, 2012 18:17
Core Data -willSave example
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSError *error = nil;
// Load the data model
NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];
// Set up the SQLite store
NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [documentsDirectory URLByAppendingPathComponent:@"Example.sqlite"];
@pnc
pnc / AppDelegate.m.patch
Created April 6, 2012 18:05
Fix to Marcus Zarra's consecutive migration code
--- 1.txt 2012-04-06 14:09:33.000000000 -0400
+++ 2.txt 2012-04-06 14:09:45.000000000 -0400
@@ -119,28 +119,35 @@
}
//END:progressivelyMigrateURLFindModels
//See if we can find a matching destination model
//START:progressivelyMigrateURLFindMap
NSMappingModel *mappingModel = nil;
@pnc
pnc / PCPropertyObserver.c
Created August 31, 2012 14:36
PCPropertyObserver
// © 2011 Phillip N. Calvin
// For use in a setter.
// Signs up self to receive KVO notifications about given properties
// on any new value and removes self as an observer from the old value.
// For example, to observe properties of a property called customer
// (backed by ivar _customer):
// PCPropertyObserver(customer, @"allowSubscriptionPurchase", @"availableProducts");
// You'd need to write this selector yourself:
@pnc
pnc / NSDictionary+Inflector.h
Created November 5, 2012 16:00
(Possibly broken) camel-case / underscore inflection in Objective-C
#import <Foundation/Foundation.h>
@interface NSDictionary (Inflector)
- (NSDictionary *)underscoredDictionaryWithPrefix:(NSString *)prefix andSuffix:(NSString *)suffix;
@end
@pnc
pnc / awful-network.md
Last active October 12, 2015 11:08
Randomize link quality

Creating an awful Wi-Fi network

  1. Plug an Ethernet cable into your computer. You'll need this for Internet connectivity while your Wi-Fi card is used to create a network.

  2. Enable Internet Sharing in System Preferences.

  3. Get your phone connected to the network you just created and make sure you can browse the web.

  4. Ensure dummynet'd packets are still routed through the normal rules so masquerading works:

     eleos@Nebula ~$ sudo sysctl -w net.inet.ip.fw.one_pass=0
     net.inet.ip.fw.one_pass: 0 -> 0
    
@pnc
pnc / install_resources.sh
Created November 8, 2012 12:39
Get CocoaPods (almost) to compile xcdatamodel resources as it copies them, like it does XIBs
*.xcdatamodel)
echo "`basename $1 .xcdatamodel`.mom"
echo "`basename ${1} .xcdatamodel`.mom"
xcrun momc "${PODS_ROOT}/$1" "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/`basename ${1} .xcdatamodel`.mom"
;;
@pnc
pnc / section.m
Created February 1, 2013 14:51
Example of using ATV.
ATVManagedTableSection *inboxSection = [[ATVManagedTableSection alloc] initWithIdentifier:@"inbox"];
[inboxSection setManagedObjectContext:context andFetchRequest:fetchRequest];
[inboxSection registerNib:@"PIMessageCell" forIdentifier:@"PIMessageCell"];
[inboxSection setCellSource:^UITableViewCell *(ATVTableSection *section, NSUInteger index, id object) {
UITableViewCell *cell = [section dequeueReusableCellWithIdentifier:@"PIMessageCell"];
return cell;
}];
[inboxSection setConfigureCell:^(ATVTableSection *section, UITableViewCell *cell, NSUInteger index, id object) {
PIMessageCell *messageCell = (PIMessageCell *)cell;
[messageCell setMessage:object];