Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@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
@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 <stefan.rusterholz@gmail.com> - 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 / 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
@apeiros
apeiros / progressbar.rb
Created January 28, 2012 09:18
A progressbar in the command line
# Usage
# bar = ProgressBar.new
# bar.init
# 0.step(100, 0.5) { |i|
# bar.update(i)
# sleep(rand*0.1)
# }
# bar.finish
#
# you can update the progress and render independently if you wish. Use
@apeiros
apeiros / inspector.rb
Created November 17, 2012 15:23
Inspect objects which redefined to_s
# encoding: utf-8
# Sadly, in ruby <2.0 Object#inspect uses to_s, and will use a redefined to_s. Not even
# Object.instance_method(:inspect).call(obj) gets you around that.
# This module provides something akin to Object#inspect for arbitrary objects.
#
# @example Generic object inspection
# puts Inspector.inspect_object(some_obj)
#
# @example Including it into a class
@apeiros
apeiros / ruby_string_scrubbed_utf_8.rb
Last active December 18, 2015 07:39
Clean a string from invalid byte sequences.
class String
# Defaults for String#scrubbed_utf8's options argument
ScrubbedUtf8Defaults = {invalid: :replace, undef: :replace}
# Similar to what String#encode does, the options argument is also the same,
# but it defaults to :replace for both :invalid and :undef options.
#
# :invalid:
# If the value is :replace, #encode replaces invalid byte sequences in str
# with the replacement character. The default is :replace.
@apeiros
apeiros / assert_unordered_equal.rb
Created January 28, 2014 21:18
Assert two enumerables to be equal, not minding the order
module Test::Unit::Assertions
def assert_unordered_equal(expected, actual, message=nil)
full_message = build_message(message, "<?> expected but was\n<?>.\n", expected, actual)
assert_block(full_message) {
seen = Hash.new(0)
expected.each { |e| seen[e] += 1 }
actual.each { |e| seen[e] -= 1 }
seen.invert.keys == [0]
}
end
@apeiros
apeiros / hash_map.rb
Created March 13, 2014 12:13
Implement Hash#map_keys, #map_keys!, #map_values, #map_values!, #map_pairs and #map_pairs!
class Hash
def map_keys
map_pairs { |key, value| [yield(key), value] }
end
def map_keys!
map_pairs! { |key, value| [yield(key), value] }
end
def map_values