Skip to content

Instantly share code, notes, and snippets.

View davidbalbert's full-sized avatar

David Albert davidbalbert

View GitHub Profile

Keybase proof

I hereby claim:

  • I am davidbalbert on github.
  • I am davidbalbert (https://keybase.io/davidbalbert) on keybase.
  • I have a public key whose fingerprint is C59D 970A 2A49 D78B ACE7 6722 F447 4DD8 FC80 DAB5

To claim this, I am signing this object:

@davidbalbert
davidbalbert / hmm_om.cljs
Created July 7, 2014 01:57
A curious case.
(def hmm-state (atom {:value "Hmm..."}))
(defn hmm [state owner]
(reify
om/IRender
(render [_]
(om.dom/textarea #js {:value (:value state)
:onChange (fn [e] (.log js/console (.. e -target -value)))}))))
(om/root
slashes = ["|", "/", "-", "\\"]
slashes.cycle do |s|
system "clear"
puts ":#{s}"
sleep 0.3
end
@davidbalbert
davidbalbert / community_import.rb
Last active August 29, 2015 14:04
Google Groups -> Community import script
# This will be pasted into the REPL
require 'csv'
# these were extracted from the first line of the CSV
GoogleGroupContact = Struct.new(:email_address, :nickname, :group_status, :email_status, :email_preference, :posting_permissions, :join_year, :join_month, :join_day, :join_hour, :join_minute, :join_second, :time_zone)
class InteractiveContactFinder
def self.find_all(contacts)
@@done = false
@davidbalbert
davidbalbert / remove_dupes.rb
Created September 2, 2014 19:49
Community: Destroy duplicate visited statuses
# Returns the number of user+thread combinations with more than one visited
# status, this should be greater than zero before the fix and zero after the
# fix.
VisitedStatus.group([:user_id, :thread_id]).having("COUNT(*) > 1").count.size
# The number of user+thread combinations with at least one visited status. This
# number should remain the same before and after the fix.
VisitedStatus.group([:user_id, :thread_id]).count.size
# The fix: Group all visited statuses by user+thread, select the visited statuses.
module Enumerable
def reductions(sym = nil, &block)
raise LocalJumpError, "no block given" unless sym || block
if sym
block = sym.to_proc
end
arr = to_a

This is a partial implementation of Piumarta and Warth's Open, extensible object models paper in Ruby. It is the simpler implementation where bind returns a method rather than a closure that wraps the method and some arbitrary piece of data. It also doesn't implement any method caching. I'm not super happy with the the split between in the "low level" Ruby object system, and the "higher level" system that we're implementing, but I don't have time to do a redesign before publishing today's PotW :).

Running the program will drop you into a shell where you can play with the object model. It's nicer if you have pry installed because then you'll have access to local variables like symbol and s_delegated.

@davidbalbert
davidbalbert / not.rb
Last active August 29, 2015 14:21
not.rb: fun with monkey patching with Zach Allaun
class Object
def not
NotProxy.new(self)
end
end
class NotProxy
def initialize(obj)
@obj = obj
end
@davidbalbert
davidbalbert / ubiq
Last active August 29, 2015 14:23
Ubiq.im command line client. Share Spotify and Rdio links with everyone.
#!/usr/bin/env ruby
if ARGV.empty?
$stderr.puts("usage: #{$0} rdio-or-spotify-url")
exit 1
end
require 'net/http'
require 'uri'
@davidbalbert
davidbalbert / tsrange_tz_conversion_bug.rb
Last active August 29, 2015 14:26
Active Record tsrange timezone conversion bug
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'