Skip to content

Instantly share code, notes, and snippets.

module Visitable
def accept object
object.visit self
end
end
class Visitor
def visit thing
send(:"visit_#{thing.class.name}", thing)
end
class Object
def send_through(object, *args)
object.dispatcher_for(self).call(self, *args)
end
end
module Dispatcher
class DispatcherNotFound < StandardError; end
def self.extended(klass)
@mike-burns
mike-burns / examples.rb
Created October 24, 2012 13:24
Stop typoing #initialize
class User < Params.over(:first_name, :last_name)
def info
"I am a user: #{@first_name} #{@last_name}"
end
end
class Admin < User.params(:awesomeness_level)
def info
"I am a level #{@awesomeness_level} admin: #{@first_name} #{@last_name}"
end
@jessegrosjean
jessegrosjean / AppDispatcher.js
Last active December 23, 2015 01:03
Reflux style wrapper around Flux
var copyProperties = require('react/lib/copyProperties'),
Dispatcher = require('flux').Dispatcher,
util = require('util');
function AppDispatcher() {
Dispatcher.call(this);
this._queue = [];
}
util.inherits(AppDispatcher, Dispatcher);
@smsohan
smsohan / rename_angular_parts
Last active April 28, 2016 15:55
AngularJS renaming exercise
Because I think the hardest part of getting started with AngularJS is it's esoteric names, I'm suggesting the following replacements of the AngularJS terminology.
scope -> state
factory -> data/models
filter -> helper
directive {restrict: 'E'} -> custom_tag
directive {restrict: 'A'} -> custom_attribute
@olivierlacan
olivierlacan / .pryrc
Created January 5, 2012 01:16 — forked from renz45/.pryrc
Pry configuraton
# switch default editor for pry to sublime text
Pry.config.editor = "sublime"
# format prompt to be <Rails version>@<ruby version>(<object>)>
Pry.config.prompt = proc do |obj, level, _|
prompt = "\e[1;30m"
prompt << "#{Rails.version} @ " if defined?(Rails)
prompt << "#{RUBY_VERSION}"
"#{prompt} (#{obj})>\e[0m"
end
@hardbap
hardbap / upgrade_postgres.txt
Last active December 14, 2016 01:28
Upgrading postgresql from 9.4.x to 9.5.x with Hstore
# Orignal instructions here: https://kkob.us/2016/01/09/homebrew-and-postgresql-9-5/
1. Stop postgresql
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
2. Install postgresql 9.5
$ brew update && brew upgrade postgresql
3. Make a new 9.5 database
$ initdb /usr/local/var/postgres9.5 -E utf8
@rweald
rweald / simple-linear-regression.rb
Created August 29, 2012 19:13
Simple Linear Regression in Ruby
class SimpleLinearRegression
def initialize(xs, ys)
@xs, @ys = xs, ys
if @xs.length != @ys.length
raise "Unbalanced data. xs need to be same length as ys"
end
end
def y_intercept
mean(@ys) - (slope * mean(@xs))
@adomokos
adomokos / visitor_pattern_example.rb
Created May 24, 2011 17:28
The Visitor Pattern implementation in Ruby from the Wikipedia example
class CarElement
def accept(visitor)
raise NotImpelementedError.new
end
end
module Visitable
def accept(visitor)
visitor.visit(self)
end
@igrigorik
igrigorik / gist:3148848
Created July 20, 2012 05:24
Convert any YouTube video into an audio file you can listen to on the go...
# Convert any YouTube video into an audio file you can listen to on the go, using:
# http://rg3.github.com/youtube-dl/
{ ~ } > brew install ffmpeg
{ ~ } > wget https://raw.github.com/rg3/youtube-dl/2012.02.27/youtube-dl
{ ~ } > chmod u+x youtube-dl
# Pick which video format you want to download.. (use any YT video link)
{ ~ } > ./youtube-dl -s -F http://www.youtube.com/watch?v=vT1KmTQ-1Os