Skip to content

Instantly share code, notes, and snippets.

require 'ostruct'
class MyOpenStruct < OpenStruct
def _to_hash
h = @table
#handles nested structures
h.each do |k,v|
if v.class == MyOpenStruct
h[k] = v._to_hash
end
@forforf
forforf / multi_delegator.rb
Created March 4, 2011 22:30
A class that can delegate class and instance methods to multiple delegates
#classes to delegate to
#############
class A
class << self; attr_accessor :_me; end
attr_accessor :me
def initialize
@me = :aa
end
@forforf
forforf / Hash Creation
Created March 17, 2011 19:03
Add to a hash whether the hash exists or not
def create_hash(akey, avalue, ahash)
ahash ||= {}
ahash.merge!({akey => avalue}) if akey
ahash
end
p create_hash(:a, 'A', {:z => ['z', 'Z']})
@forforf
forforf / comprehensionsPatterns.coffee
Created July 22, 2011 16:19
Comprehension Patterns for Coffeescript
#map equivalent
x = for i in ['a', 'b', 'c']
i + i
#=> ['aa', 'bb', 'cc']
#for each equivalent
for i in ['a', 'b', 'c']
#do side effect
null
@forforf
forforf / couchdb-ec2-install.sh
Created September 16, 2011 13:36 — forked from msmith/couchdb-ec2-install.sh
Set up CouchDB on EC2
#!/bin/bash
#
# This script installs and configures couchdb on a fresh Amazon Linux AMI instance.
#
# Must be run with root privileges
# Tested with Amazon Linux AMI release 2011.02.1.1 (ami-8c1fece5)
#
export BUILD_DIR="$PWD"
@forforf
forforf / neorefactor2.rb
Created October 10, 2011 01:43
NEO-Refactoring 2
module NeoMethods
#include NeoProxy
#ThinProxy objects must be used when the object doesn't support #define_singleton_method (i.e. symbols)
#SymbolMethods = {:sym_add => lambda{|x| result = (self.to_s + x.to_s).to_sym; replace_self(result)} }
module Symbol
Methods = {
:sym_add => lambda do |x|
#proxy = ThinProxy.new(self)
sum = self.to_s + x.to_s
@forforf
forforf / recursive_lambdas.rb
Created October 13, 2011 14:45
Recursive Lambda Functions
#Self Application Function
lambda {|f| f.call(f)}
g_x = lambda {|g_x|
lambda { |*args|
args
}
}
p lambda {|f| f.call(f)}.call(g_x).call(:a, :b, :c)
@forforf
forforf / Hash-find_deep.rb
Created October 24, 2011 16:43
Find nested values within a hash
#find arbitrairly deep value based on keys
# {:a=> {:b=> {:c => 'C'}}}.find_deep(:a, :b, :c) #=> 'C'
class Hash
def find_deep(*keys)
find_deep_iter(self, keys)
end
def find_deep_iter(h, keys)
return h if keys.empty?
if h.respond_to? :keys
@forforf
forforf / unbind_tap_bind.rb
Created October 28, 2011 02:52
Unbind, destroy and recreate a method at runtime. Meta-programming goodness
#This captures the Array#sort method and stores it in m
m = Array.instance_method(:sort)
#Shows that sort still works
[3,2,1].sort
#=> [1,2,3]
#Here we override the array sort method
#Array#sort no longer works
[3,2,1].class.send(:define_method, :sort){nil}
@forforf
forforf / wrap_require.rb
Created November 8, 2011 21:22
Wrapped Require
module Kernel
alias :old_req :require
def require(*args)
puts "Require called with: #{args}"
old_req *args
end
end
require 'pp'