Skip to content

Instantly share code, notes, and snippets.

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 / 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
@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 / 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 / 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
# 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 / examples.rb
Created January 15, 2011 23:19
A generic smaller/bigger implementation, for things like infinite ranges etc.
(1..Bigger).first(5) # => [1, 2, 3, 4, 5]
@apeiros
apeiros / gist:634082
Created October 19, 2010 12:01
Terminate current process the hard way (kill -9)
module Kernel
# Terminate the current process - the hard way
def t!
`kill -9 #{$$}`
end
module_function :t!
end
unless YAML.respond_to?(:dump_file) then
def YAML.dump_file(path, content)
File.open(File.expand_path(path), 'wb') do |fh|
fh.write(content.to_yaml)
end
end
class Object
def to_y(path=nil)
path ? YAML.dump_file(path, self) : to_yaml
@apeiros
apeiros / pool.rb
Created August 26, 2010 21:10
Thread::Pool
require 'thread'
class Thread
# Example:
# pool = Thread::Pool.new(10) do |exception| handle(exception) end
# pool.execute(1,2,3) do |x,y,z| whatever(x,y,z) end
# pool.join
class Pool