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
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] |
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
# [ 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 |
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
#!/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 |
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
class Rake::Task | |
def overwrite(&block) | |
abandon | |
enhance(&block) | |
end | |
def abandon | |
@actions.clear | |
prerequisites.clear |
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 CurrentController | |
def self.included(base) | |
base.before_filter :set_current_controller | |
end | |
def set_current_controller | |
Thread.current[:current_controller] = self | |
end | |
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
module CurrentMethodName | |
def current_method_name | |
caller[0][/`([^']*)'/, 1] | |
end | |
end | |
# Example: | |
# | |
# class MyClass | |
# include CurrentMethodName |
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 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}); |
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 AuthenticityToken | |
def authenticity_token(&block) | |
class_inheritable_accessor :authenticity_token_generator | |
self.authenticity_token_generator = block | |
include InstanceMethods | |
end | |
module InstanceMethods |
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
<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> |
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 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 |
OlderNewer