Skip to content

Instantly share code, notes, and snippets.

View jacobo's full-sized avatar

Jacob Burkhart jacobo

  • HOVER
  • San Francisco, CA
View GitHub Profile
@jacobo
jacobo / gist:5166690
Created March 15, 2013 00:59
confused by sleep
require 'celluloid'
class DiCaprio
include Celluloid
def do_a_thing
puts "starting"
`sleep 1`
puts "finished"
end
class TrapLoop
trap('TERM') { stop! }
trap('INT') { stop! }
trap('SIGTERM') { stop! }
def self.stop!
@loop = false
end
def self.safe_exit_point!
ERROR: no command given
Usage: test_worker <command> <options> -- <application options>
* where <command> is one of:
start start an instance of the application
stop stop all instances of the application
restart stop all instances and restart them afterwards
run start the application and stay on top
zap set the application to a stopped state
@jacobo
jacobo / gist:5391690
Created April 15, 2013 22:10
debugging in the console
class SimpleViz
def initialize(app, &lookup)
@app = app
end
def call(env)
puts caller
request = Rack::Request.new(env)
puts "-> #{request.request_method.upcase} (#{request.url})"
status, headers, body = @app.call(env)
puts "<--#{status}-- #{body.size}"
@jacobo
jacobo / gist:5393037
Created April 16, 2013 03:01
re-create datamapper database in console
DataMapper.repository(@repository).adapter.extend(SQL::Mysql)
DataMapper.repository(@repository).adapter.recreate_database
require 'celluloid'
class Worker
include Celluloid
def work(x)
sleep 0.2; puts x*x
end
end
worker_pool = Worker.pool(size: 10)
(0..99).to_a.each{|x| worker_pool.async.work x }
sleep 2 #how do I wait the right amount of time before terminating?
@jacobo
jacobo / block_refinement.rb
Last active December 17, 2015 22:19
In http://rubykaigi.org/2013/talk/S58 @Shogu describes how refinements work, but says he would rather they work with a block-like syntax. I asked why he couldn't just implement these extensions in pure ruby using the same techniques that stub and mock libraries use. The translated answer was "Because I want to refinement only to apply inside the…
class BlockRefinement
def self.refines(*classes_refined)
@classes_refined = classes_refined
end
def self.refines_method(method_refined, &new_method)
@classes_refined.each do |class_refined|
old_method = class_refined.instance_method(method_refined)
@reverts ||= []
@jacobo
jacobo / alternate_refinements.rb
Created May 31, 2013 01:37
Yet another example of implementing something LIKE refinements directly in pure ruby. I run into the contraint of being unable to properly set 'self' in my blocks. (could be solved if I had https://bugs.ruby-lang.org/issues/8465) AND, it's very hacky because it uses caller. but I *think* this is what @Shogu generally intends the semantics of blo…
class BlockRefinement
def self.refines(*classes_refined)
@classes_refined = classes_refined
end
def self.refines_method(method_refined, &new_method)
@classes_refined.each do |class_refined|
old_method = class_refined.instance_method(method_refined)
@reverts ||= []
require 'rubygems'
require 'nokogiri'
str = "<select id=\"cluster_configuration_data_snapshot_id\" name=\"cluster_configuration[data_snapshot_id]\"></select>"
#This one fails
failing = Proc.new do
innards = "<option data-size=\"15\" value=\"\" volume_size=\"\">New volume</option>\n<option class=\"time\" data-html-suffix=\" (snap-f2e4f551) (Application Master) [64-bit]\" data-size=\"15\" datetime=\"2013-06-20T20:29:32+00:00\" value=\"8\" volume_id=\"14\" volume_size=\"15\" selected>\nThu Jun 20 20:29:32 2013 (snap-f2e4f551) (Application Master) [64-bit]\n</option>"
x = Nokogiri::XML::Element.new('select', Nokogiri::XML(str))
x.inner_html = innards
@jacobo
jacobo / gist:5870675
Last active December 19, 2015 00:39
There should be a button in github UI for doing this...
git remote add upstream https://github.com/resque/resque.git
git fetch upstream
git checkout master
git merge upstream/master
git push origin master