Skip to content

Instantly share code, notes, and snippets.

View betawaffle's full-sized avatar

Andrew Hodges betawaffle

View GitHub Profile
@betawaffle
betawaffle / config.ru
Created October 25, 2011 16:59
Rack-based Local YARD Server
require 'rubygems'
require 'yard'
libs = {}
gems = {}
base = ENV['GEM_PATH'].split(':')[1][%r{^.+(?=/.+@global$)}] rescue nil
if base
Gem.paths = {
'GEM_PATH' => Dir["#{base}/ruby-*"].join(':'),
redefine_on_receiver do |old|
lambda do |*args|
args[0] = "\e[0;31m#{args[0]}\e[0m"
old.call(*args)
end
end
I just stuck that in the spec_helper.rb.
File.expand_path('~/etc/pry').tap do |base|
%w(core_ext custom_commands).each { |f| require File.join(base, f) }
end
require File.expand_path('~/etc/pry/pry-rspec.rb')
require File.expand_path('~/etc/pry/pry-helpers.rb')
Pry.config.should_load_plugins = false
Pry.config.auto_indent = false
Pry.config.correct_indent = false
FIB = [0, 1]
def fib(n, seed = nil)
seed = FIB.take(n).each { |x| puts x } unless seed
seed << (seed[-1] + seed[-2]).tap { |x| puts x; FIB << x } if seed.size == n
while s = seed.size and s < n
fib(s, seed)
end
-spec timestamp(atom(), binary(), object()) -> object().
%% Facebook Timestamp (eg. <<"2011-11-21T22:37:44+0000">>)
timestamp(Field, <<Year:4/binary, $-, Month:2/binary, $-, Day:2/binary, $T,
Hour:2/binary, $:, Minute:2/binary, $:, Second:2/binary, Offset:5/binary>>, Acc = #fb_object{}) ->
Date = {?BIN_TO_INT(Year), ?BIN_TO_INT(Month), ?BIN_TO_INT(Day)},
Time = {?BIN_TO_INT(Hour), ?BIN_TO_INT(Minute), ?BIN_TO_INT(Second)},
DateTime = case Offset of
<<"+0000">> -> % UTC
{Date, Time};
_ ->
app = Rails.application
session_store_class = app.config.session_store
begin
app = app.instance_variable_get :@app
end until app.class == session_store_class
# Allows you to easily make a full closure in ruby
# Examples:
def bork
x = 'abc'
closure { x }
end
f = bork
f.call
module Enumerable
def lazy_map mapper = nil, &block
return to_enum(:lazy_map, block) unless mapper
return to_enum(:lazy_map, mapper) unless block
each do |*args|
yield mapper.call(*args)
end
end
end
# in config/initializers
ActiveRecord::Relation.class_eval do
include EachRow
end