Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / gist:229001
Created November 8, 2009 00:54
Treat a File like a String
FileString now lives in its own repository at http://github.com/apeiros/filestring
@apeiros
apeiros / gist:234189
Created November 13, 2009 21:36 — forked from dwt/gist:234180
class Object
def chain_method(with_symbol, &new_method)
old_method = method with_symbol
eigenclass = class << self; self; end
eigenclass.class_eval do
define_method with_symbol do |*args| new_method[old_method, *args] end
end
end
end
@apeiros
apeiros / FIGHT!.txt
Created December 11, 2009 07:51 — forked from epitron/FIGHT!.txt
$ time ruby1.8 bits.rb 16 > /dev/null
real 0m9.861s
user 0m8.977s
sys 0m0.872s
$ time ruby1.9 bits.rb 16 > /dev/null
real 0m1.742s
user 0m1.740s
# This code is under BSD 3-clause license
# Author: Stefan Rusterholz <stefan.rusterholz@gmail.com> (aka apeiros)
module Kernel
ValidLocalVariableName = /\A[a-z_]\w*\z/
# Dynamically define a bunch of local variables from a hash.
# Overrides existing local variables.
#
# IMPORTANT: This is an anti-pattern. Think thrice before using this. It
#!/usr/bin/ruby -w
module Kernel
ValidLocalVariableName = /\A[a-z_]\w*\z/
def empty_binding
proc{ binding }.call
end
# Dynamically define a bunch of local variables from a hash.
#--
# Copyright 2007 by Stefan Rusterholz.
# All rights reserved.
# See LICENSE.txt for permissions.
#++
require 'erb'
#!/usr/bin/env ruby
require 'logger' # requires belong *outside*
module LogInterceptors
LOGGER = Logger.new(STDOUT)
LOGGER.level = Logger::DEBUG
def log_all
module Yaffle
module ForActiveRecordBase # better names welcome
def acts_as_something
...do some stuff, e.g. define scopes and associations...
extend MyActsAs::ClassMethods
include MyActsAs::InstanceMethods
end
end
module ClassMethods
unless ENV["BUNDLE_GEMFILE"] then
if $stdout.tty? # Only do an interactive "do you really want to" if we're in an interactive session
require 'timeout'
Timeout.timeout 30 do
puts "\e[1;41m WARNING \e[0;41mYou're running `#{$0}` instead of `bundle exec #{$0}` \e[0m"
print "Proceed? [yN] "
$stdout.flush
abort "Terminated" unless $stdin.gets.chomp =~ /\Ay(?:es)?\z/i
end
else # not interactive, just terminate with a non-zero status
class ReverseSortProxy
attr_reader :value
def initialize(value)
@value = value
end
def <=>(other)
other.value <=> @value
end
end