Skip to content

Instantly share code, notes, and snippets.

View avdi's full-sized avatar

Avdi Grimm avdi

View GitHub Profile
# :PROCESS: ruby, "ruby %f 2>&1"
# :PROCESS: markdown, "rdiscount"
# :PUBLISHER: blog, atompub
# :PUBLISHER: source, gist
# :BRACKET_CODE: "[ruby]", "[/ruby]"
# :TEXT:
# In parts 1 and 2 of this series, we look at some of Ruby's built-in ways to
# start subprocesses. In this article we'll branch out a bit, and examine some
# of the tools available to us in Ruby's Standard Library. In the process,
@avdi
avdi / Makefile
Created October 17, 2009 20:42
Avdi's WebOS Makefile
APPNAME = org.avdi.myapp
VERSION = 1.0.0
DEVICE = tcp
PACKAGEFILE = $(APPNAME)_$(VERSION)_all.ipk
VMNAME = "Palm Emulator (1.2.0.33)"
EMUPORT = 5522
M4 = m4
PACKAGE = palm-package
INSTALL = palm-install
INSTALLFLAGS = -d $(DEVICE)
require 'tempfile'
def browse_string(body)
Tempfile.open('browse_string') do |f|
browser = ENV.fetch('BROWSER') { 'firefox' }
f.write(body)
f.close
puts "Starting browser #{browser} with response..."
unless system(browser, f.path)
warn "Error starting browser #{browser}"
@avdi
avdi / rails.el
Created October 22, 2009 04:56
Horrible elisp hack to keep flymake from confusing Merb, Rails, etc.
;; Hack up emacs-rails/flymake integration to make autotest happy
;; adapted from flymake.el
(defun abg-flymake-create-temp-inplace-without-ext (file-name prefix)
(unless (stringp file-name)
(error "Invalid file-name"))
(or prefix
(setq prefix "flymake"))
(let* ((temp-name (concat (file-name-sans-extension file-name) "." prefix)))
(flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name)
require 'right_aws'
require 'progressbar'
module Simpledb
# Count the objects in a domain
def sdb_count(domain, options={})
results = session.select("select count(*) from #{domain}")
results[:items].first["Domain"]["Count"].first.to_i
end
@avdi
avdi / example.rb
Created October 30, 2009 22:28
Rack::Stereoscope
require 'sinatra'
require 'json'
require 'addressable/uri'
require File.expand_path('rack_stereoscope.rb', File.dirname(__FILE__))
configure do
use Rack::Reloader;
use Rack::Lint;
use Rack::Stereoscope;
end
def map_hash_to_hash(original, options={})
original.inject({}){|a, (key,value)|
catch(:skip) do
value = if (options[:deep] && Hash === value)
map_hash_to_hash(value) {|k,v| yield(k, v) }
else
value
end
new_key, new_value = yield(key,value)
a[new_key] = new_value
def transform_hash(original, options={}, &block)
original.inject({}){|result, (key,value)|
value = if (options[:deep] && Hash === value)
transform_hash(value, options, &block)
else
value
end
block.call(result,key,value)
result
}
# Single-level flatten
arr = [ [1], [2,3], [4, [5]] ]
# Too flat!
arr.flatten # => [1, 2, 3, 4, 5]
# Just right!
arr.inject([]){|a,e| a.concat(e)} # => [1, 2, 3, 4, [5]]
export EDITOR="/home/avdi/share/bin/emacs-newwindow"