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
FileString now lives in its own repository at http://github.com/apeiros/filestring |
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 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 |
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
$ 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 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
# 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 |
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 | |
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. |
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
#-- | |
# Copyright 2007 by Stefan Rusterholz. | |
# All rights reserved. | |
# See LICENSE.txt for permissions. | |
#++ | |
require 'erb' |
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/env ruby | |
require 'logger' # requires belong *outside* | |
module LogInterceptors | |
LOGGER = Logger.new(STDOUT) | |
LOGGER.level = Logger::DEBUG | |
def log_all |
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 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 |
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
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 |
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 ReverseSortProxy | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
def <=>(other) | |
other.value <=> @value | |
end | |
end |