Skip to content

Instantly share code, notes, and snippets.

Stefan Rusterholz apeiros

Block or report user

Report or block apeiros

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@apeiros
apeiros / hash_map.rb
Created Mar 13, 2014
Implement Hash#map_keys, #map_keys!, #map_values, #map_values!, #map_pairs and #map_pairs!
View hash_map.rb
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
@apeiros
apeiros / assert_unordered_equal.rb
Created Jan 28, 2014
Assert two enumerables to be equal, not minding the order
View assert_unordered_equal.rb
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 / ruby_string_scrubbed_utf_8.rb
Last active Dec 18, 2015
Clean a string from invalid byte sequences.
View ruby_string_scrubbed_utf_8.rb
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 / inspector.rb
Created Nov 17, 2012
Inspect objects which redefined to_s
View inspector.rb
# 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 / progressbar.rb
Created Jan 28, 2012
A progressbar in the command line
View progressbar.rb
# 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 / literalparser.rb
Created Jul 22, 2011
Parse literals to ruby objects
View literalparser.rb
require 'strscan'
require 'bigdecimal'
# This is copied and slightly refactored from BareTest::TabularData
#
# Example
# LiteralParser.parse("nil") # => nil
# LiteralParser.parse(":foo") # => :foo
@apeiros
apeiros / dirtyhash.rb
Created Jul 8, 2011
A hash preserving changes made to it
View dirtyhash.rb
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 / gist:898050
Created Apr 1, 2011
sort_by with asc/desc
View gist:898050
## sortby.rb
module SortBy
class Reverse
include Comparable
attr_reader :value
def initialize(value)
@value = value
end
@apeiros
apeiros / transliteration.rb
Created Feb 18, 2011
Unicode String operations, case mapping, natural sorting
View transliteration.rb
# 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 / pool.rb
Created Aug 26, 2010
Thread::Pool
View pool.rb
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
You can’t perform that action at this time.