Skip to content

Instantly share code, notes, and snippets.

# encoding: utf-8
require 'spreadsheet'
require 'stringio'
class Spreadsheet::Workbook
class Namespace
def initialize(current=[], &block)
@current = current
instance_eval(&block)
end
def namespace(name, &block)
Namespace.new(@current+[name], &block)
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
def ==(other)
other.data == @data
rescue NoMethodError
false
end
class String
def match_all(regex)
if block_given?
scan(regex) { yield $~ }
else
enum_for(:scan, regex).map { $~ }
end
end
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
@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 / regex_literal_1.rb
Created March 26, 2014 21:56
Regex, encodings, invalid sequences
# encoding: utf-8
#p(/\xff/.encoding) # -> raises SyntaxError, "invalid multibyte escape: /\xff/"
p(/\xff/n.encoding)
#p(/\xff/u.encoding) # -> raises SyntaxError, "invalid multibyte escape: /\xff/"
p(/x/.encoding)
p(/x/n.encoding)
p(/x/u.encoding)
#!/usr/bin/ruby -w
# grab the input, convert it, sum it, and spit out the answer
$stdin.each_line do |line|
puts line.scan(/\d+/).map(&:to_i).reduce(:+)
end