Skip to content

Instantly share code, notes, and snippets.

pg.exec_params("CREATE TABLE foos (data bytea)")
para = Array.new(256).pack("C*") # => "\x00\x01\x02\x03\x04\x05…\xFE\xFF"
pg.exec_params("INSERT INTO foos ($1)", [para])
pg.exec_params("SELECT * FROM foos").to_a # => [{"data"=>"\\x"}]
@apeiros
apeiros / submitForm.js
Created September 9, 2014 06:31
send a "virtual" form with values from JS
;(function($) {
// Sends a (virtual) form with values.
// Unlike an AJAX Post/Get, this actually makes the browser send the form and process the response. Which means that
// the site is being changed (or a file will be downloaded)
//
// Example usage:
// $.submitForm('/my/resource', {'someFormParam': 'andItsValue'}, {'method': 'PUT'});
//
// Notes:
@apeiros
apeiros / servr.rb
Created July 25, 2014 13:28
servr.rb
require 'webrick'
include WEBrick
s = HTTPServer.new(
:Port => 9951,
:DocumentRoot => File.expand_path(File.dirname(__FILE__))
)
s.config[:MimeTypes].update({
"text" => "text/plain",
})
trap("INT") { s.stop }
@apeiros
apeiros / unreachable.rb
Created July 25, 2014 11:45
Mark code as unreachable
class Unreachable < StandardError
end
module Kernel
module_function def unreachable(msg="This place in the code should never have been reached")
raise Unreachable, msg
end
end
@apeiros
apeiros / column_dsl.rb
Last active August 29, 2015 14:01
How to break up the method definition line, so the line is not too long?
module Jacob
class Model
class ColumnDsl
# [snip]
def integer(name, null: false, default: nil, size: nil, primary_key: false, auto_increment: false, unique: false)
# [snip]
end
# [snip]
@apeiros
apeiros / crypto.rb
Last active August 29, 2015 14:00
What's wrong in crypto - compare
### The stuff to encrypt
data = 'hello world!'
password = 'test123'
# Note: all 3 implementations are supposed to do exactly the same
### Raw OpenSSL
# Decisions, decisions, decisions...
cipher = 'AES-256-CBC' # which ciphers are not trivially broken?
require "openssl"
require 'digest/sha2'
require 'securerandom'
# Automatically upgradable encryption provider
module Encryption
class InvalidData < StandardError
end
class InvalidPattern < InvalidData
def initialize(version)
def that_method(parameter)
array_list = ['Thing', 'Another Thing']
array_list.include? parameter
end
#!/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
@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)