Skip to content

Instantly share code, notes, and snippets.

View mboeh's full-sized avatar

Matthew Boeh mboeh

View GitHub Profile
@mboeh
mboeh / snatcher.rb
Created January 9, 2013 04:15
Gillian, please use extreme caution.
class Snatcher < BasicObject
C = ::Object.new
class << C
def abduct_method(meth, arity)
ObjectSpace.each_object(Module) do |mod|
next if mod.kind_of? Class
if mod.instance_methods.include?(meth)
return mod.instance_method(meth)
end
#!/usr/bin/env ruby
require 'open-uri'
require 'rexml/document'
@mboeh
mboeh / clarify.txt
Created September 26, 2012 22:13
just for the sake of clarification
What I mean is that even though JS method invocation seems to work like this:
(obj.method)(args)
it's a bit trickier than that. See this:
> a = {}
> a.toString()
"[object Object]"
> (a.toString)()
@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 / 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 = {}