Skip to content

Instantly share code, notes, and snippets.

View cadwallion's full-sized avatar
⚒️
Building all the things

Andrew Nordman cadwallion

⚒️
Building all the things
View GitHub Profile
@cadwallion
cadwallion / gist:4043168
Created November 9, 2012 01:36
Mruby hello world interpreting
#include <iostream>
#include <mruby.h>
#include <mruby/proc.h>
#include <mruby/compile.h>
void eval_expanded(mrb_state *mrb, char code[]) {
struct mrb_parser_state *p;
mrbc_context *context = mrbc_context_new(mrb);
p = mrb_parse_string(mrb, code, context);
@cadwallion
cadwallion / gist:4003172
Created November 2, 2012 17:56
Terrible Use of Begin/Rescue for Platform Initializers
begin
platform_detection
rescue OSXFoundError
osx_specific_initializer
rescue WindowsFoundError
windows_specific_initializer
rescue UnixFoundError
unix_specific_initializer
ensure
universal_initializer
@cadwallion
cadwallion / equipment.rb
Created October 6, 2012 23:39
YAML Equipment config
require 'yaml'
equipment = YAML.load_file('equipment.yml')
weapons = equipment['weapons']
armor = equipment['armor']
puts weapons[1][1] # => { 'name' => 'Long Sword', 'damage' => 7 }
@cadwallion
cadwallion / gist:3431526
Created August 23, 2012 02:30
BracketTree Template Generator
module BracketGenerator
class Node < Struct.new(:left, :right, :position, :depth, :render)
def initialize
self.render = true
end
def add
self.left = Node.new
self.right = Node.new
end
@cadwallion
cadwallion / gist:3090907
Created July 11, 2012 14:51
Random Animal Sounds
class Dog
def speak
"woof"
end
end
class Cat
def speak
"fuck you"
end
@cadwallion
cadwallion / foo_spec.rb
Created June 25, 2012 15:09
RSpec include_kind_of
require 'spec_helper'
module Bar
def bar ; end
end
class Foo
include Bar
end
@cadwallion
cadwallion / unicorn.rb
Created June 5, 2012 03:35
Unicorn config
rails_env = ENV['RAILS_ENV'] || 'production'
app = 'my_awesome_app'
# 4 workers and 1 master
worker_processes (rails_env == 'production' ? 4 : 1)
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
@cadwallion
cadwallion / beer_menu.thor
Created May 15, 2012 18:50
Ruck beer list scraper
#!/usr/bin/env ruby
# A Thor-based commandline tool for all BeerMenu.
# Based on @jsteiner's Ruby equivalent of @jlet's Python Ruck
# beer list scraper
# @jsteiner's Original: https://gist.github.com/2703889
# @jlet's Original: ttps://gist.github.com/2659721
require 'open-uri'
require 'nokogiri'
@cadwallion
cadwallion / bracket_serializer.rb
Created May 2, 2012 13:54
ActiveModel::Serializer fails w/ Array of Hashes
# This is a problem with an array of Hash objects wherein each Hash object
# needs to be manipulated rather than all keys serialized.
#
# @bracket.class # => Bracket
# @bracket.seats.class # => Array
# @bracket.seats[0].class # => Hash
# @bracket.seats[0].keys # => ['position','round', 'won']
class BracketSerializer < ActiveModel::Serializer
attribute :final_size, key: :size
@cadwallion
cadwallion / README.md
Created April 30, 2012 01:37
ActiveModel::Serializer Observations/edge-cases

Observation #1 (see included code):

App has 4 keys in a hash property called stats: '10s', '1min', '5min', 'day'. Each are arrays of Stat objects In order to create a stats attribute in AppSerializer, we have to create an intermediary Serializer with the attributes in order to re-use StatSerializer for each of those four Stat collections, and the syntax within that Serializer is pretty kludgy to make the association work.

Observation #2: