Skip to content

Instantly share code, notes, and snippets.

module Rack
# Bounces or redirects requests to missing static files.
# Partially inspired by [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)
#
# I.e. could be useful when you want to run the server with production database locally
# and have user uploaded content fetched transparently from production site.
#
# Options:
# :mode - [ :off,
# :bounce, # returns 404 to any request to static URL,
@dolzenko
dolzenko / includable_with_options.rb
Created April 1, 2010 20:39
include modules passing options
# Check Thomas Sawyer take on the problem http://github.com/rubyworks/paramix
module IncludableWithOptions
class << self
attr_accessor :last_default_options
end
def self.included(includable_with_options)
%w(string/methodize kernel/constant module/basename module/spacename).each { |facets_core_ext| require "facets/#{ facets_core_ext }" }
raise "IncludableWithOptions should be included by the Module" unless includable_with_options.instance_of?(Module)
@dolzenko
dolzenko / try_block.rb
Created April 8, 2010 08:23
Exceptions based guarded method invocation or glorified `rescue nil`
module TryBlock
def self.install!
unless Object.include?(self)
Object.send(:include, self)
Object.send(:public, :try_block)
end
end
def self.is_nil_exception_message?(message)
message =~ /You have a nil object when you didn't expect it/ ||
@dolzenko
dolzenko / io_interceptor.rb
Created April 19, 2010 22:00
Safe way to intercept writes to IO stream
# Safe way to intercept IO stream
# where just replacing STDOUT doesn't work:
# http://rubyforge.org/tracker/index.php?func=detail&aid=5217&group_id=426&atid=1698
#
module IoInterceptor
def intercept
begin
@intercept = true
@intercepted = ""
yield
@dolzenko
dolzenko / shell_out.rb
Created April 20, 2010 00:44
Shell out using pseudo-tty where available
# ## ShellOut
#
# Provides a convenient feature-rich way to "shell out" to external commands.
# Most useful features come from using `PTY` to execute the command. Not available
# on Windows, `Kernel#system` will be used instead.
#
# ## Features
#
# ### Interruption
#
@dolzenko
dolzenko / global_net_http_debug.rb
Created April 20, 2010 15:45
global_net_http_debug.rb
require "net/http"
Net::HTTP.module_eval do
def D(msg)
(@debug_output ||= STDERR) << msg << "\n"
end
end
@dolzenko
dolzenko / gist_readme.rb
Created April 20, 2010 15:57
gist_readme.rb
#!/usr/bin/env ruby
#
# Usage: ./gist_readme.rb source_file.rb [README.md]
#
# Extracts first comment block from source_file.rb into README.md file
#
#
raise "source_file should be specified" if ARGV.empty?
File.open(File.expand_path(File.join("../", ARGV[1] || "README.md") , __FILE__), "w") do |f|
@dolzenko
dolzenko / error_print.rb
Created April 22, 2010 15:38
Formats the Exception so that it looks *familiar*, i.e. exactly like your interpreter does it.
# Formats the Exception so that it looks *familiar*,
# i.e. exactly like your interpreter does it.
#
# Port of MRI native `error_print` function.
class Exception
def self.error_print(e)
warn_print = ""
backtrace = e.backtrace
backtrace = [ backtrace ] if backtrace.is_a?(String) # 1.9 returns single String for SystemStackError
@dolzenko
dolzenko / symmetrically_checking_for_passed_block.rb
Created April 28, 2010 06:27
Symmetrically checking for passed block
def without_block_arg
if defined?(yield)
yield(42)
else
# no block passed
end
end
def with_block_arg(&block)
if block