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 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 |
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
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 |
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 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. |
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 | |
# 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 |
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
# 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 |
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
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
## 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 | |
# 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
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 |
NewerOlder