Skip to content

Instantly share code, notes, and snippets.

Chris O'Sullivan thechrisoshow

View GitHub Profile
@thechrisoshow
thechrisoshow / imphash.patch
Created Feb 20, 2018
This is a patch to Ruby 2.5.0 to enable es6 like hash literals - instructions in the comments
View imphash.patch
From a034498e36c02715b9201646d4ce1ddcddf118c3 Mon Sep 17 00:00:00 2001
From: Shugo Maeda <shugo@ruby-lang.org>
Date: Wed, 29 Apr 2015 09:14:15 +0900
Subject: [PATCH] support ES6-like hash literals.
---
parse.y | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
1 file changed, 72 insertions(+), 17 deletions(-)
diff --git a/parse.y b/parse.y
View interruptible_sleep.rb
# Shamelessly stolen from https://gist.github.com/ileitch/1459987
module InterruptibleSleep
def interruptible_sleep(seconds)
@sleep_check, @sleep_interrupt = IO.pipe
IO.select([@sleep_check], nil, nil, seconds)
end
def interrupt_sleep
@sleep_interrupt.close if @sleep_interrupt
View banana_array.m
@interface BananaBox : NSObject
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
@end
@implementation AppleCart
NSMutableArray *_bananas;
-(id) init {
self = [self init]
View banana_dictionary.m
@interface Banana : NSObject
- (id)objectForKeyedSubscript:(id)key;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
@end
@implementation Banana
NSMutableDictionary *_attributes;
-(id) init {
self = [self init];
@thechrisoshow
thechrisoshow / special_methods.m
Created Sep 21, 2012
Special methods for adding subscripting
View special_methods.m
// To add array style subscripting:
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx; // setter
- (id)objectAtIndexedSubscript:(NSUInteger)idx; // getter
// To add dictionary style subscripting
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key; // setter
- (id)objectForKeyedSubscript:(id)key; // getter
@thechrisoshow
thechrisoshow / new_literal.m
Created Sep 21, 2012
New literal subscripting in objective c
View new_literal.m
NSArray *anArray = @[a,b,c];
NSDictionary *aDictionary @{ a : a1, b : b1};
// New subscripting format
anArray[0]; // => returns a
aDictionary[a] // => returns a1
@thechrisoshow
thechrisoshow / new_accessors.m
Created Sep 5, 2012
If you want to add the new array/dictionary accessors to IOS5
View new_accessors.m
// Put this in your ..-Prefix.pch file
// Add support for subscripting to the iOS 5 SDK.
#if __IPHONE_OS_VERSION_MAX_ALLOWED < 60000
@interface NSObject (SubscriptingSupport)
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj forKeyedSubscript:(id <NSCopying>)key;
- (id)objectForKeyedSubscript:(id)key;
@thechrisoshow
thechrisoshow / account.rb
Created Jul 26, 2012
How to best represent a field that uses multiple inputs with activerecord
View account.rb
# The problem I have is that I've got a sort_code field in the database, and I want to keep that as one field
# BUT I want users to edit it in the form using 3 different fields.
# My initial solution was to use virtual attributes and divide it this way.
# Can you think of a better way?
class Account < ActiveRecord::Base
def sort_code_one
sort_code[0..1]
@thechrisoshow
thechrisoshow / dynamic_validations.rb
Created Mar 29, 2012
How to add validations to a specific instance of an active record object?
View dynamic_validations.rb
class Banana < ActiveRecord::Base; end
banana = Banana.new
banana.valid? #=> true
banana.singleton_class.validates_presence_of :name
banana.valid? #=> true - why did the validation not work?
banana.class.validates_presence_of :name
banana.valid? #=> false - as we'd expect...but now...
@thechrisoshow
thechrisoshow / application_controller_spec.rb
Created Mar 5, 2012
Use of anonymous controller in rspec tests
View application_controller_spec.rb
require 'spec_helper'
describe ApplicationController do
describe "current_user" do
controller do
before_filter :authorize
def index
end
You can’t perform that action at this time.