Skip to content

Instantly share code, notes, and snippets.

# An example implementation of caching
# WARNING: this implementation does not feature any pruning/expiration strategy,
# be careful not to create a memory leak
require 'thread'
module Cacheable
@global_cache = {}
@mutex = Mutex.new
@apeiros
apeiros / transliteration.rb
Created February 18, 2011 19:13
Unicode String operations, case mapping, natural sorting
# Encoding: utf-8
# The following code is under BSD 2-clause license
# -> http://en.wikipedia.org/wiki/BSD_licenses#2-clause_license_.28.22Simplified_BSD_License.22_or_.22FreeBSD_License.22.29
#
# Author: Stefan Rusterholz <[email protected]> - https://github.com/apeiros/
#
# A module to help with transliteration issues.
# Provides methods for:
# * Changing the case of characters/strings, mapping most latin characters
@apeiros
apeiros / gist:898050
Created April 1, 2011 12:10
sort_by with asc/desc
## sortby.rb
module SortBy
class Reverse
include Comparable
attr_reader :value
def initialize(value)
@value = value
end
@apeiros
apeiros / irbrc
Created April 16, 2011 23:32
First draft of my rewritten irbrc - use with caution...
# encoding: utf-8
if defined?(Encoding) then
Encoding.default_external = 'utf-8'
Encoding.default_internal = 'utf-8'
else
$KCODE = 'utf-8'
end
ENV["LANG"] = 'en_US.UTF-8'
@apeiros
apeiros / float_approximative_equality_key.rb
Created May 1, 2011 20:07
Float approximations in ruby
class Float
ExponentBits = 11 # 11 bits for exponent
SignificandBits = 52 # 52 bits for significand
ApproximativeEqualityDefault = 37 # precise to about 5 decimal places, which should be good for anything non-scientific
ApproximativeMaxExponent = 1<<ExponentBits
ApproximativeMaxSignificand = 1<<SignificandBits
ApproximativeMask1 = (1<<11)-1
ApproximativeMask2 = (1<<20)-1
ApproximativeMask3 = (1<<32)-1
h,m,s = *"23:34:02".match(/\A(\d+):(\d\d):(\d\d)\z/).captures
Time.now+h.to_i*3600+m.to_i*60+s.to_i
@apeiros
apeiros / seeds.rb
Created May 26, 2011 17:22
A beefed up seeds file for rails
require 'pp'
puts "Seeding for env '#{Rails.env}'"
# disable AR logger
old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil unless $VERBOSE
env_seed_file = "#{Rails.root}/db/data/seed/#{Rails.env.downcase}/seeds.rb"
# first load yaml files that is "base" loading
@apeiros
apeiros / dirtyhash.rb
Created July 8, 2011 19:27
A hash preserving changes made to it
require 'hash/dirty'
# DirtyHash behaves just like Hash, but keeps track of changes applied to it.
#
# @example
# dh = DirtyHash.with :x => 1
# dh.clean? # => true
# dh.update :y => 2, :z => 4
# dh.changed # => {:y => [:added, 2], :z => [:added, nil, 4]}
# dh[:z] = 3
@apeiros
apeiros / literalparser.rb
Created July 22, 2011 20:13
Parse literals to ruby objects
require 'strscan'
require 'bigdecimal'
# This is copied and slightly refactored from BareTest::TabularData
#
# Example
# LiteralParser.parse("nil") # => nil
# LiteralParser.parse(":foo") # => :foo
class Module
def swap_method(a, b)
x = instance_method(a)
y = instance_method(b)
define_method(b, x)
define_method(a, y)
end
end