Skip to content

Instantly share code, notes, and snippets.

View thechrisoshow's full-sized avatar

Chris O'Sullivan thechrisoshow

View GitHub Profile
@thechrisoshow
thechrisoshow / imphash.patch
Created February 20, 2018 22:53
This is a patch to Ruby 2.5.0 to enable es6 like hash literals - instructions in the comments
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
@thechrisoshow
thechrisoshow / interruptible_sleep.rb
Last active March 19, 2018 18:11
Heroku style spinner
# 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
@interface BananaBox : NSObject
- (id)objectAtIndexedSubscript:(NSUInteger)idx;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx;
@end
@implementation AppleCart
NSMutableArray *_bananas;
-(id) init {
self = [self init]
@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 September 21, 2012 14:28
Special methods for adding subscripting
// 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 September 21, 2012 14:26
New literal subscripting in objective c
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 September 5, 2012 15:18
If you want to add the new array/dictionary accessors to IOS5
// 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 July 26, 2012 14:09
How to best represent a field that uses multiple inputs with activerecord
# 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 March 29, 2012 11:56
How to add validations to a specific instance of an active record object?
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 March 5, 2012 14:25
Use of anonymous controller in rspec tests
require 'spec_helper'
describe ApplicationController do
describe "current_user" do
controller do
before_filter :authorize
def index
end