Skip to content

Instantly share code, notes, and snippets.

View thechrisoshow's full-sized avatar

Chris O'Sullivan thechrisoshow

View GitHub Profile
@thechrisoshow
thechrisoshow / upload.rb
Created June 5, 2011 13:03
This script uploads files to a S3 bucket
require 'rubygems'
require 'aws/s3'
# Change the access key and secret to your amazon credentials
AWS::S3::Base.establish_connection!(
:access_key_id => 'ACCESS_KEY_ID',
:secret_access_key => 'SECRET'
)
# Change this to your S3 bucket name
WEBSITE_BUCKET_NAME = "YOUR_BUCKET_NAME"
@thechrisoshow
thechrisoshow / whitelabeling.rb
Created August 16, 2011 10:43
How should I white label a Rails app?
class ApplicationController < ActionController::Base
around_filter :set_white_label
private
def set_white_label
subdomains = request.subdomains - RESERVED_SUBDOMAINS
if subdomains.empty?
yield
return
@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 / 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
@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
When /^I reload the page$/ do
page.evaluate_script("window.location.reload()");
end
@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