Skip to content

Instantly share code, notes, and snippets.

@mikbe
mikbe / md.sh
Created March 8, 2011 14:59
Bash: Make a directory and change into it in one easy command
#!/bin/bash
# You should add this to your .profile file
md () { mkdir -p "$@" && cd "$@"; }
@mikbe
mikbe / chd.sh
Created March 8, 2011 16:17
Bash: Change to a directory given a full or partial name
#!/bin/bash
# Change to a directory given a full or partial name
# You don't have to wrap directory names in quotes either
chd() {
if [ -n "$@" ]; then
if [ -n "`ls "$@"* 2>/dev/null`" ]; then
cd "$@"*
else
echo "-bash: chd: "$@": No directory could be extrapolated from partial name"
fi
@mikbe
mikbe / gist:1012758
Created June 7, 2011 17:57
spec/ruby/library/socket/tcpsocket/open_spec.rb
bin/mspec spec/ruby/library/socket/tcpsocket/open_spec.rb
rubinius 1.2.4dev (1.8.7 90126691 yyyy-mm-dd JI) [x86_64-apple-darwin10.7.0]
....E.
1)
TCPSocket.open with a running server connects to a server when passed local_host and local_port arguments ERROR
Errno::EAFNOSUPPORT: Address family not supported by protocol family - bind(2)
Errno.handle at kernel/common/errno.rb:16
TCPSocket#tcp_setup at lib/socket.rb:1085
TCPSocket#initialize at lib/socket.rb:1013
@mikbe
mikbe / argf.rb
Created June 8, 2011 00:05
Change to argf - works command line, not
kernel/loader.rb : line 279
options.on "-i", "[EXT]", "Edit ARGV files in place, making backup with EXT" do |ext|
# in place edit mode
$-i = ext || true
end
kernel/common/argf.rb : line 476
def advance!
@mikbe
mikbe / thread_unsafe.rb
Created June 17, 2011 12:17
Verify thread collisions are possible
require 'thread'
class Foo
attr_accessor :bar
def baz
@bar ||= rand(10000000000)
end
end
@mikbe
mikbe / thread_safe.rb
Created June 17, 2011 13:30
Stops collisions but it's ugly
require 'thread'
module ThreadSafe
def self.included(base)
base.extend(ThreadSafeClassMethods)
base.threadsafe_class_mutex = Mutex.new
end
module ThreadSafeClassMethods
@mikbe
mikbe / ugly_spec.rb
Created July 6, 2011 01:20
What's a better way to test if a block is passed on?
it "should pass along blocks if given" do
class Baz3
def initialize(attributes = nil, &block)
yield if block_given?
end
end
class Qiz3 < Baz3
include Eventable
end
@mikbe
mikbe / class_mutex.rb
Created July 6, 2011 21:59
Class level mutex
require 'benchmark'
module SuperMutex
attr_reader :bar
def self.included(base)
base.extend(ThreadSafeClassMethods)
base.threadsafe_class_mutex = Mutex.new
end
@mikbe
mikbe / super_created.rb
Created July 6, 2011 22:02
Super call creates the mutex
require 'benchmark'
module SuperMutex
attr_reader :bar
def initialize
@mutex = Mutex.new
end
@mikbe
mikbe / foo.c
Created July 9, 2011 14:36
Sample task info for current task
#include <stdio.h>
#include <mach/mach_init.h>
#include <mach/mach_port.h>
#include <mach/task_info.h>
#include <mach/thread_act.h>
#include <mach/vm_map.h>
#include <mach/task.h>
#include <sys/types.h>