Skip to content

Instantly share code, notes, and snippets.

['mysql2', 'sequel', 'pp'].each(&method(:require))
DB = Sequel.connect('mysql2://root@localhost/tmp')
DB.tables.each {|t| DB.drop_table(t) }
def t(name, columns, *rows)
DB.create_table(name) do
columns.each.with_index do |column, ind|
type = rows[0][ind].class.to_s
send type, column
end
class SSHKit::Backend::Netssh
alias_method :command_orig, :command
def command(*a)
@user = 'test2' # TODO replace with user to sudo to
command_orig(*a)
ensure
@user = nil
end
end
module Net
class HTTP
alias_method '__initialize__', 'initialize'
def initialize(*args,&block)
__initialize__(*args, &block)
ensure
@debug_output = $stderr ### if ENV['HTTP_DEBUG']
end
end
ruby -rwebrick -e'WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: Dir.pwd).start'
@dolzenko
dolzenko / mgmt-deps.rb
Last active August 29, 2015 14:16
Prints missing/unused Go deps for https://github.com/bsm/mgmt
require 'json'
require 'set'
myself = `go list`.strip
deps = Set.new
`go list ./...`.each_line do |dep|
pdeps = JSON.parse(`mgmt go list -json #{dep}`)
deps += pdeps['Deps']
deps += pdeps['TestImports'] if pdeps['TestImports']
package main
import "fmt"
func f() (int, error) {
return 42, nil
}
func main() {
x := 3
@dolzenko
dolzenko / acts_as.rb
Created June 4, 2009 08:52
My attempt at abstracting acts_as_* pattern
class Class
def acts_as(*args)
modules_with_options = []
for arg in args
if arg.is_a?(Module)
modules_with_options << [arg]
elsif arg.is_a?(Hash)
raise ArgumentError, "Options without module" unless modules_with_options[-1][0].is_a?(Module)
modules_with_options[-1][1] = arg
end
@dolzenko
dolzenko / sum_with_enumerable_fallback
Created October 19, 2009 15:05
sum_with_enumerable_fallback
ActiveRecord::Associations::AssociationCollection.class_eval do
def sum_with_enumerable_fallback(*args, &block)
if args.empty? && block
to_a.sum(&block)
else
sum_without_enumerable_fallback(*args)
end
end
alias_method_chain :sum, :enumerable_fallback
@dolzenko
dolzenko / Dead Simple JavaScript Class Pattern (jQuery compatible).js
Created October 27, 2009 20:39
Dead Simple JavaScript Class Pattern (jQuery compatible)
var MyClass = function() {
var self = this;
$.extend(self, {
initialize: function(constructor_arg1, constructor_arg2) {
self.method();
},
method: function() {
}
// other methods below...
@dolzenko
dolzenko / parse_rails_log_file_using_enumerable_slice_before.rb
Created March 9, 2010 19:49
parse_rails_log_file_using_enumerable_slice_before.rb
def parse_rails_log_file(file)
# Remove all empty lines
lines = file.each_line.map(&:strip).reject(&:empty?)
# Use +Enumerable#slice_before+ to slice log file into sections for each request
lines.slice_before(/Started (GET|POST|PUT|DELETE)/).each_with_object({}) do |request_log, totals|
# Only include successfully finished actions in report
if duration = request_log.last[/Completed 200 OK in (?<duration>\d+)ms/, :duration]
action_name = request_log.first[/Started (GET|POST|PUT|DELETE) "(?<action>.+?)" for/, :action]
totals[action_name] ||= 0