Skip to content

Instantly share code, notes, and snippets.

require 'rinda/tuplespace'
# Workers will be initialized a Rinda::TupleSpace
# (which we can use as a stand-in for Redis
class MapReduceWorker
def initialize(tuplespace)
@ts = tuplespace
@name = "MapReduceJob"
end
@dmcclory
dmcclory / bacon.html
Created April 16, 2014 01:01
Bacon.js example
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/bacon.js/0.7.10/bacon.js"></script>
</head>
<body>
<div>Running total: <span id="amount"></span></div>
<div id="ledger">
<input type="text" id="entry">
<button>Awesome!</button>
@dmcclory
dmcclory / local_variables.rb
Created June 19, 2014 23:03
get a hash of local variables from a binding
def hash_for_binding(b)
vars = b.eval('local_variables')
vars.inject({}) { |memo, var|
memo[var] = b.eval(var.to_s)
memo
}
end
@dmcclory
dmcclory / ruby_hoist.rb
Created July 3, 2014 21:01
Ruby Hoists Variables!
class Foo
def awesome(x)
y = 50
x + y
end
end
set_trace_func proc { |event, file, line, id, binding, classname|
# output the binding at the start of any call to Foo#awesome

Keybase proof

I hereby claim:

  • I am dmcclory on github.
  • I am dan_promptworks (https://keybase.io/dan_promptworks) on keybase.
  • I have a public key whose fingerprint is E3A6 C750 8DCC 2234 D142 ACAE 93EF 1804 FFEF 69AD

To claim this, I am signing this object:

# some real quick code for running requests in parallel
# usage:
# $ ruby parallel_req.rb http://url_place/ post 5 data.json
# doesn't work with get requests (yet)!
require 'http'
url = ARGV[0]
method = ARGV[1]
thread_count = ARGV[2].to_i
@dmcclory
dmcclory / podcast_spike.html
Created September 15, 2015 20:09
Javascript control of audio elements
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
</head>
<body>
<h1>Yo a Podcast episode!</h1>
<audio controls src="http://traffic.libsyn.com/ucblongform/Karin_Louise_Hammerberg.mp3" type="audio/mpe
g">
Your browser does not support the <code>audio</code> element.
</audio>
@dmcclory
dmcclory / pathname_collections.rb
Created August 22, 2012 22:08
functions which return all the files or all of the directories contained within a Pathname
def collect_files(pn)
if pn.file?
return pn
end
pn.children.collect {|cn| collect_files(cn)}.flatten
end
def collect_directories(pn)
if pn.directory?
pn.children.collect {|cn| collect_directories(cn)}.flatten.reject{|i| i.nil?}.push(pn)
@dmcclory
dmcclory / copy_folder.rb
Created August 23, 2012 21:23
Copying the contents of one directory to another
# you want to recursively copy the contents of one folder to another
# but you don't want to actually copy the first folder into the second
#3 ways to try to copy the contents of one directory to another:
require 'pathname'
require 'fileutils'
include FileUtils
p = Pathname.new('input')
o = Pathname.new('output')
@dmcclory
dmcclory / gist:3908513
Created October 17, 2012 21:49
escape a list of elements
CGI.escapeElement( Ox.dump(@doc), "p", "div", "span", ... )