This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#classes to delegate to | |
############# | |
class A | |
class << self; attr_accessor :_me; end | |
attr_accessor :me | |
def initialize | |
@me = :aa | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create_hash(akey, avalue, ahash) | |
ahash ||= {} | |
ahash.merge!({akey => avalue}) if akey | |
ahash | |
end | |
p create_hash(:a, 'A', {:z => ['z', 'Z']}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Kernel | |
alias :old_req :require | |
def require(*args) | |
puts "Require called with: #{args}" | |
old_req *args | |
end | |
end | |
require 'pp' |
OlderNewer