Skip to content

Instantly share code, notes, and snippets.

View mboeh's full-sized avatar

Matthew Boeh mboeh

View GitHub Profile
@mboeh
mboeh / log.txt
Created August 21, 2012 17:18 — forked from steveklabnik/log.txt
A fun shell script from #euruku
$ history 1 | awk {'print $2, $3, $4'} | sort | uniq -c | sort -k1 -rn | head -n 30
172 fg
164 git st
163 ls
72 ruby create_event_local.rb blah
62 git push
59 git pull
54 git commit -m
50 git di
47 bundle
@mboeh
mboeh / gogo.rb
Created August 7, 2012 18:09
thread and GC nuance
# Summary: In MRI 1.9, if you keep threads in an array in a local variable, the threads may outlive the local variable.
# I have not yet been able to produce a scenario in which the threads are ever garbage-collected.
# You have to remove the thread from the array before it goes out of scope, e.g. using #pop or #clear.
# INPUT DESIRED: If anyone can demonstrate a condition in which a local array keeps threads and those threads are eventually GC'd.
# INPUT DESIRED: Is this expected behavior or a bug?
# NOTE: This test will not work as expected in JRuby, because its garbage collection works differently. Whether the same behavior exists in JRuby is an exercise for someone smarter than I am.
class Foo
THREAD_PROC = lambda{ (0..10).to_a.map do Foo.new end }
@mboeh
mboeh / add1.rb
Created August 2, 2012 00:32
add1
# http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html
def add1(arr, val, n)
range = (0 ... arr.length)
indexes = n < 0 ? range.reverse_each : range.each
n = n.zero? ? arr.length : n.abs
indexes.each do |idx|
if arr[idx] == val
arr[idx] += 1
@mboeh
mboeh / nodup.rb
Created July 31, 2012 06:05
#dup and #clone are so rude!
class Object
private :dup
private :clone
private :freeze
end
module ValueObject
def self.included(into)
into.send :public, :dup
into.send :public, :clone
@mboeh
mboeh / README.markdown
Created July 19, 2012 23:23
Memoization and Threads

This shows how different approaches to memoization work (or don't) in different Ruby engines.

Synopsis

If you're using the idiomatic Ruby approach to memoization, like this:

def data
  @memo ||= expensive_action
end
@mboeh
mboeh / backdroppify
Created July 13, 2012 21:51
backdrop scripts
#!/bin/sh
# backdroppify - sizes an image to the minimum size which will fill your entire screen & trims overflow
bgsize=`xwininfo -root | grep -- -geometry | cut -d' ' -f4 | cut -d+ -f1`
convert $1 -resize $bgsize^ -gravity center -extent $bgsize ~/.backdrop.jpg
set-backdrop
@mboeh
mboeh / as_proc.rb
Created July 13, 2012 19:24
Attempt at converting lambdas to have proc behavior
require 'test/unit/testcase'
require 'test/unit' if $0 == __FILE__
class Proc
def as_proc
if arity == -1
self
else
aty = arity.abs - (arity < 0 ? 1 : 0)
require 'celluloid'
require 'zip/zipfilesystem'
require 'uri'
require 'net/http'
class ZipManifest
include Celluloid
def initialize(filename)
@urls = {}
@mboeh
mboeh / single-arg.rb
Created March 19, 2012 21:55
Single-argument #[]=
class Orz
attr_reader :v
def []=(v)
@v = v
end
end
orz = Orz.new
orz[] = :a
orz.v # ==> :a
@mboeh
mboeh / rtdocumentation.rb
Created March 19, 2012 19:31
Runtime documentation for Ruby classes
# NOTE: I just slapped this together as a proof of concept. Thread safety etc. is left to the reader.
module RTDocumentation
def self.included(into)
# method_added doesn't seem to work if mixed in
into.send(:define_method, :method_added) do |name|
if @_rtdoc_nextdoc
@_rtdocs ||= {}
@_rtdocs[name.to_s] = @_rtdoc_nextdoc