Skip to content

Instantly share code, notes, and snippets.

@stephanschubert
stephanschubert / RGB to CMYK with caching
Last active August 30, 2015 14:24
Convert CMYK to RGB with automatic caching
cmyk = Hash.new do |cache, rgb|
cmy = rgb.map { |color| 255 - color }
k = cmy.min
cache[rgb] = cmy.map { |color| color - k } + [k]
end
# Usage
cmyk[ [128, 42, 23] ] # => [0, 86, 105, 127]
# [ Fixnum, Float ].each{ |klass| klass.send :include, Extensions::NetAndGross }
#
# Examples:
# 1.net # => 1
# 1.gross # => 1.19
# 1.gross.gross # => 1.19
# 1.gross.gross.net # => 1.0
#
module Extensions
#!/usr/bin/ruby
%w(rubygems socket yaml).each { |lib| require lib }
class Bot
attr_reader :nick, :server, :port, :options, :server_info, :server_message
def initialize(nick, server, port, options = {}, &block)
@nick = nick
@server = server
@port = port
class Rake::Task
def overwrite(&block)
abandon
enhance(&block)
end
def abandon
@actions.clear
prerequisites.clear
@stephanschubert
stephanschubert / current_controller.rb
Created November 6, 2009 11:34
A small Ruby module which provides access to the current controller everywhere you could need it. Works in multithreaded environments.
module CurrentController
def self.included(base)
base.before_filter :set_current_controller
end
def set_current_controller
Thread.current[:current_controller] = self
end
end
@stephanschubert
stephanschubert / current_method_name.rb
Created November 18, 2009 12:11
Ruby module providing convenient access to the currently called method's name
module CurrentMethodName
def current_method_name
caller[0][/`([^']*)'/, 1]
end
end
# Example:
#
# class MyClass
# include CurrentMethodName
@stephanschubert
stephanschubert / google_analytics_real_bounce_rate_fix.rb
Created November 21, 2009 12:05
Track your real bounce rate by triggering a custom no-bounce event with Google Analytics after a given timeout.
def google_analytics_real_bounce_rate_fix(timeout = 10)
name = "NoBounce"
msg = "Over #{timeout} seconds"
ms = timeout * 1000
<<-JS
function disqualifyAsBounce() {
pageTracker._trackEvent('#{name}', '#{name}', '#{msg}');
}
setTimeout("disqualifyAsBounce()", #{ms});
@stephanschubert
stephanschubert / authenticity_token.rb
Created December 1, 2009 11:58
Ruby module for convenient authenticity token generation and usage for any class
module AuthenticityToken
def authenticity_token(&block)
class_inheritable_accessor :authenticity_token_generator
self.authenticity_token_generator = block
include InstanceMethods
end
module InstanceMethods
@stephanschubert
stephanschubert / apache_asset_config
Created December 2, 2009 18:16
Apply a cachebuster to your static assets before using this Apache configuration
<FilesMatch "\.(pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
Header set Cache-Control "public"
ExpiresActive On
ExpiresDefault "access plus 10 years"
FileETag None
Header unset Last-Modified
Header unset ETag
</FilesMatch>
@stephanschubert
stephanschubert / gist:296073
Created February 5, 2010 18:39
Recursive fibonacci with memoization
def memoize(name)
cache = {}
(class << self; self; end).send(:define_method, name) do |*args|
cache[args] = super(*args) unless cache.has_key?(args)
cache[args]
end
end
def fib(n)
return n if n < 2