This file contains hidden or 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
# 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 |
This file contains hidden or 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
# 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 |
This file contains hidden or 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
## sortby.rb | |
module SortBy | |
class Reverse | |
include Comparable | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
This file contains hidden or 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
# 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' |
This file contains hidden or 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 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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
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 |
This file contains hidden or 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
require 'strscan' | |
require 'bigdecimal' | |
# This is copied and slightly refactored from BareTest::TabularData | |
# | |
# Example | |
# LiteralParser.parse("nil") # => nil | |
# LiteralParser.parse(":foo") # => :foo |
This file contains hidden or 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 Module | |
def swap_method(a, b) | |
x = instance_method(a) | |
y = instance_method(b) | |
define_method(b, x) | |
define_method(a, y) | |
end | |
end |