Skip to content

Instantly share code, notes, and snippets.

@kirel
kirel / 0-readme.md
Last active December 18, 2015 05:48 — forked from burke/0-readme.md
modified to use falcon postfix

ruby-1.9.3-p327 cumulative performance patch for rbenv

This installs a patched ruby 1.9.3-p327 with various performance improvements and a backported COW-friendly GC, all courtesy of funny-falcon.

Requirements

You will also need a C Compiler. If you're on Linux, you probably already have one or know how to install one. On OS X, you should install XCode, and brew install autoconf using homebrew.

# scope uses merge which looses conditions when chained on the same attribute
# see https://github.com/rails/rails/issues/7365 for the issue
# see https://github.com/rails/rails/blob/da5e5c5f779355a2e99e63a90612cbeaeb0fc986/activerecord/lib/active_record/relation/spawn_methods.rb#L35 for an explanation
# see https://github.com/rails/rails/commit/cd26b6ae7c1546ef8f38302661bbedf8cb487311 for a fix in Rails 4
# The workaround is to just use class methods - we wrap a dsl around just that
# Usage: extend ChainableScope in your ActiveRecord::Base derived class
module ChainableScope
def chainable_scope name
raise 'chainable_scope must be called with a block!' unless block_given?
define_singleton_method name do |*args|
@kirel
kirel / attribute_casts.rb
Last active December 17, 2015 19:39
AttributeCasts
require 'active_support/concern'
module AttributeCasts
extend ActiveSupport::Concern
module ClassMethods
# casts method => { writer: type, reader: type}
# or
# casts method => type # equiv to casts method => { writer: type }
# type needs to respond to new or to_proc
# thus primarily Classes, Procs and Symbols can be used for casting
def casts casts = {}
@kirel
kirel / astrid2evernote.scpt
Created May 26, 2013 11:06
Import from astrids export tasks.csv into an Evernote notebook "Test"
on csvToList(csvText, implementation)
-- The 'implementation' parameter must be a record. Leave it empty ({}) for the default assumptions: ie. comma separator, leading and trailing spaces in unquoted fields not to be trimmed. Otherwise it can have a 'separator' property with a text value (eg. {separator:tab}) and/or a 'trimming' property with a boolean value ({trimming:true}).
set {separator:separator, trimming:trimming} to (implementation & {separator:",", trimming:false})
script o -- Lists for fast access.
property qdti : getTextItems(csvText, "\"")
property currentRecord : {}
property possibleFields : missing value
property recordList : {}
end script
# coding: utf-8
require 'awesome_print'
words = %w[bla blub]
letters = words.sample.split('') # get a random word
solution = letters.size.times.map { '_' }
lifes = 10
def draw(lifes, solution)
puts "#{lifes} Leben übrig"
# Reimplementation of ActiveRecord::Store to be compatible with Enumerize.
# Explanation:
# Enumerize defines it's accessors on some module that is then included into the class. This works well
# for i.e. AR because accessors are in the method search path before AR attributes.
# ActiveRecord::Store defines accessors directly on the class thus Enumerize accessors are shadowed.
# This implementation defines store accessors on another module instead. As long as this one is mixed in before
# the Enumerize module everything works as expected.
# That means `store` has to be called and set up before `enumerize`. Same goes for the corresponding includes/extends.
module EnumerizeCompatibleStore
extend ActiveSupport::Concern
module AttributeDefaults
extend ActiveSupport::Concern
module ClassMethods
# sets default values for accessors
# defaults are returned whenever an accessor would return nil
# defaults method => default_value
def defaults defaults = {}
defaults.each do |meth, default|
default_proc = default.respond_to?(:to_proc) ? default.to_proc : proc { default }
define_method "#{meth}_default" do
class A
def == other
puts 'foo'
end
end
class B < A
alias_method :"__==__", :"=="
include Comparable
alias_method :"==", :"__==__"
@kirel
kirel / futures.rb
Last active December 10, 2015 01:09
Because I wanted to play with Threads...
module Deferred
class Proxy < BasicObject
def initialize(deferred, callback, errback)
@deferred, @callback, @errback = deferred, callback, errback
end
def method_missing(name, *args, &block)
::Thread.new do
begin
@callback.call(@deferred.send(name, *args, &block))
@kirel
kirel / .ackrc
Created December 19, 2012 17:09
ackrc
--smart-case
--sort-files
--follow
--color
--group
--type-add=html=.haml
--type-add=css=.sass,.scss,.less
--type-add=js=.coffee