Skip to content

Instantly share code, notes, and snippets.

@sergeych
sergeych / extract_params.rb
Created March 30, 2011 05:12
Extract Hash params from the array if the last element is a Hash
class Array
## Extract Hash params from the array with defaults if the last element is
# a Hash (otherwise returns defaults as is). To be used with *args, e.g.
#
# def function *args
# params = args.extract_params! { :defval => 'foobar' }
# ...
# end
def extract_params! defaults = {}
@sergeych
sergeych / numcode.rb
Created March 27, 2011 18:03
The user-readable huge number encoder that corrects misreadings
# encoding: utf-8
##
# The arbitrary-length positive integers to human readable form codec (serials, links and like).
# The idea is to avoid misreading/misinterpetation of symbols. For example,
# the letter 0 o and O, ir I and 1, often looks very likely. The numcode
# takes care of it using clearly distinctive characters for both English and Russian
# charset and corrects potential errors
#
# Numcode uses set 21 characters common to Russian and English to encode positive decimal
@sergeych
sergeych / hash.rb
Created June 21, 2010 08:29
Python-inspired Hash extension: index sequence getters/setters
# Python-inspired Hash extension: index sequence getters/setters
# author:: [email protected]
#
# Example:
#
# h = { '1' => 'one', '2' => 'two', 3 => 'three'}
# h[:one, :three, :two] = h[ '1', '3', '2']
# p h # => {"1"=>"one", "2"=>"two", 3=>"three", :one=>"one", :three=>nil, :two=>"two"}
#
class Hash
@sergeych
sergeych / sorted_array.rb
Created June 21, 2010 08:28
self-sorting Array with binary search and effective support for either comparing block or item's own odrer method (<)
# +Array+ that sorts itself on the fly.
#
# * <<, +push+ and +unshift+ insert new item in the right place
# place, as well as +, += and +concat+.
#
# * +index+ and +find_index+ use extremely effective binary search.
#
# * +insert+ and +[]=+ are made private to forbid order changes.
#
# It is possible to provide comparing procedure in the block passed to the