Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View radiospiel's full-sized avatar
💭
nothing to see here

eno radiospiel

💭
nothing to see here
View GitHub Profile
@radiospiel
radiospiel / gist:baa9f7f809ecab4eef63
Created September 7, 2014 10:03
go bidirectional copying
func BidirectionalCopy(conn net.Conn, channel net.Conn) {
start := time.Now()
var done = make(chan int, 1)
go copy("to ssh channel", conn, channel, done)
go copy("from ssh channel", channel, conn, done)
written := <-done
conn.Close()
channel.Close()
func copy(dest io.Writer, src io.Reader) int64 {
// If dest has a ReadFrom method, we use that. This gives us zero copy
// when copying between sockets, for example.
if reader_from_dest, ok := dest.(io.ReaderFrom); ok {
written, _ := reader_from_dest.ReadFrom(src)
return written
}
buf := make([]byte, 1024 * 16)
require "rubygems"
require "ruby2ruby"
require 'parse_tree' # gem ParseTree
#require 'ruby2ruby'
#require 'sexp_processor'
#require 'unified_ruby'
def Continuation.create(*args, &block)
cc = nil
/*
* Save this file as "kvs.js".
*
* Usage:
*
* node kvs.js set foo bar
*
* ... sets the "foo" entry to bar, and overwrites the kvs.js script with the updated data.
*
* node kvs.js set foo bar
$count = 1000000
def bench0(&block)
r = Time.now
$count.times do end
Time.now - r
end
def bench1(&block)
r = Time.now
$count.times(&block)
brew install dovecot
sudo dscl . -create /Users/_dovecot
sudo dscl . -create /Users/_dovecot UserShell /usr/bin/false
sudo dscl . -create /Users/_dovecot UniqueID 104
sudo dscl . -create /Users/_dovecot PrimaryGroupID 104
sudo dscl . -create /Users/_dovecot NFSHomeDirectory /var/empty
sudo dscl . -passwd /Users/_dovecot ''
class File
class Fingerprint
attr_reader :inode
def initialize(path)
@path = path
@inode = File.stat(@path).ino rescue nil
end
def ==(other)
@radiospiel
radiospiel / gist:1200913
Created September 7, 2011 15:39
A better empty? for AR associations
class ActiveRecord::Associations::AssociationCollection
# Does the AssociationCollection contains entries?
def empty?(*args)
if fetch_first_or_last_using_find?(args)
if !args.last.is_a?(Hash)
args.push :select => "1"
elsif (!args.last[:select])
args.last = { :select => "1" }.update(args.last)
end
@radiospiel
radiospiel / gist:1211147
Created September 12, 2011 12:31
Better warning for calling Object#id (because it includes a backtrace)
module BetterObjectIdWarning
def self.activate
return if @activated
@activated = true
Object.class_eval {
def id
return object_id if @in_object_id_warning
@in_object_id_warning = true
@radiospiel
radiospiel / gist:1213545
Created September 13, 2011 10:22
ThinkingSphinx: facetted search for ids only
class ThinkingSphinx::FacetIdsSearch < ThinkingSphinx::FacetSearch
def initialize(*args)
options = args.extract_options!
@fast_facet_names = options.delete(:facets) || raise("Missing facet names")
@fast_facet_names = [ @fast_facet_names ].flatten.map(&:to_s)
args.push options
super *args
end