Skip to content

Instantly share code, notes, and snippets.

View kballenegger's full-sized avatar

Kenneth Ballenegger kballenegger

View GitHub Profile
@kballenegger
kballenegger / ruby-warrior.rb
Created July 28, 2013 06:52
My solution to the Ruby Warrior game. :)
class Player
def initialize
@max_health = 20
@last_health = @max_health
@direction = :forward
end
def update_damage_taken
@kballenegger
kballenegger / ATQuothData.h
Created June 16, 2013 03:58
Quoth's shaaaaady data-store.
//
// ATQuothData.h
// Quoth
//
// Created by Kenneth Ballenegger on 6/15/13.
// Copyright (c) 2013 Kenneth Ballenegger. All rights reserved.
//
#import <Foundation/Foundation.h>
@kballenegger
kballenegger / dispatcher.rb
Created June 3, 2013 18:46
Problem with Ruby filesystem locks…
require 'open3'
command = 'ruby test2.rb'
(1..2).map do |n|
Thread.new do
puts "Executing scheduled command: #{command}"
output, status = Open3.capture2e(command)
unless status.success?
@kballenegger
kballenegger / lock.rb
Created June 3, 2013 16:18
Ensure script only runs once through a lock.
unless File.open('/tmp/transactions.lock', File::RDWR|File::CREAT, 0644).flock(File::LOCK_EX|File::LOCK_NB)
puts 'Cannot lock: Another instance of this script is probably running.'
exit
end
@kballenegger
kballenegger / fib.c
Created May 13, 2013 04:59
Fibonacci generator in C with Y-Combinator powered memoization.
//
// Fibonacci generator in C with Y-Combinator powered memoization.
//
// Written by Kenneth Ballenegger in 2013
//
#include <stdlib.h>
#include <stdio.h>
#include <Block.h>
@kballenegger
kballenegger / gist:5566093
Created May 13, 2013 04:05
Calculating fib(20) recursively is not very efficient...
Do it up to fib # 20...
About to generate # 20
About to generate # 19
About to generate # 18
About to generate # 17
About to generate # 16
About to generate # 15
About to generate # 14
About to generate # 13
About to generate # 12
@kballenegger
kballenegger / all.go
Last active December 16, 2015 11:39
Cryptographic exercise in Go (XOR, base64, hex, AES).
package main
import (
"errors"
"fmt"
"io/ioutil"
"strings"
)
const (
@kballenegger
kballenegger / validations.rb
Last active December 16, 2015 00:49
Validations library.
require 'validations/version'
module Validations
module ClassMethods
# Doubles as setter & getter for @validations
# This method is also the main interface to this lib.
#
def validations(&block)
@kballenegger
kballenegger / caching-lib-usage.mm
Last active December 15, 2015 21:09
A potential API for a library that abstracts and takes care of offline caching of assets.
typedef void(^CBOfflineCacheContentFetcher)(CBOfflineCacheFetchedContentCallback callback);
typedef void(^CBOfflineCacheFetchedContentCallback)(BOOL success, NSDictionary *content);
typedef void(^CBOfflineCacheCallback)(BOOL success, NSDictionary *content);
[CBOfflineCache fetchContent:@"store-view-boosts"
withFetcher:^(CBOfflineCacheFetchedContentCallback callback){
// this code does the network access
@kballenegger
kballenegger / alexis.rb
Created March 14, 2013 02:50
Average of load averages...
#!/usr/bin/env ruby
require 'scout_api'
group_name = ARGV[0]
Scout::Account.new('...', '...', '...')
all_servers = Scout::Server.all(group_name: 'API')