Skip to content

Instantly share code, notes, and snippets.

@apeiros
apeiros / keybase.md
Created March 18, 2014 21:56
Verifying myself: I am apeiros on Keybase.io.

Keybase proof

I hereby claim:

  • I am apeiros on github.
  • I am apeiros (https://keybase.io/apeiros) on keybase.
  • I have a public key whose fingerprint is 8069 6F42 1FD4 35A8 F0FD 23B6 97C0 D061 072D 0718

To claim this, I am signing this object:

@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
class String
def match_all(regex)
if block_given?
scan(regex) { yield $~ }
else
enum_for(:scan, regex).map { $~ }
end
end
end
def ==(other)
other.data == @data
rescue NoMethodError
false
end
def invoke(verb, path, expected_code, additional_params=nil)
if additional_params
response = RestClient.public_send(verb, restclient_uri(path), additional_params.to_json, core_params(verb))
else
response = RestClient.public_send(verb, restclient_uri(path), core_params(verb))
end
rescue RestClient::Exception => e
e
rescue
response
class Namespace
def initialize(current=[], &block)
@current = current
instance_eval(&block)
end
def namespace(name, &block)
Namespace.new(@current+[name], &block)
end
# encoding: utf-8
require 'spreadsheet'
require 'stringio'
class Spreadsheet::Workbook
@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 / array_split.rb
Last active January 1, 2016 19:29
Split an Array, similar to String#split
class Array
def split(*separator, include_separator: false)
if separator.empty?
if !block_given?
return enum_for(__method__) if separator.empty? && !block_given?
end
elsif block_given?
raise ArgumentError, "Must either pass a block or a separator, not both"
else
separator = separator.first