Skip to content

Instantly share code, notes, and snippets.

@nwjsmith
nwjsmith / daves_solution.rb
Created September 1, 2011 14:54
Dave's solution
[1,1,1,1,2,2,2,1,1,1,3,3,3].inject([]) {|on_boundary_array,x|
if on_boundary_array == [] || on_boundary_array[-1][0] != x
on_boundary_array = on_boundary_array + [[x]]
else
on_boundary_array[-1] << x
end
on_boundary_array
}
@nwjsmith
nwjsmith / middleware.rb
Created September 8, 2011 13:55 — forked from skippy/middleware.rb
delete vagrant vm's chef client and node from chef server on destroy
class OnDestroyMiddleware
def initialize(app, env)
@app = app
end
def call(env)
env["config"].vm.provisioners.each do |provisioner|
env.ui.info "Attempting to remove client #{provisioner.config.node_name}"
`knife client show #{provisioner.config.node_name}`
if $?.to_i == 0
commit_time ()
{
local repo=`git rev-parse --git-dir 2>/dev/null`
if [ -n "$repo" ]; then
local last=`git log --pretty=format:'%at' -1`
local now=`date +%s`
local m_since=0
((m_since=($now - $last) / 60))
def digits(string)
string.each_car.select { |character| /\d/.match(character).present? }
end
module Formattable
def format(opts)
# ...
end
end
class Basketball::PlayerRecord
extend Formattable
format :fields =>[:rebounds_total, :minutes, :assists, :points], :with => '-'
end
require 'rspec'
module Evil
def defn(method_name)
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method_name}
end
METHOD
end
end
function HashMap () {
this._items = {};
this.length = 0;
}
HashMap.prototype.get = function (key) {
if (key === null || typeof this._items[this._hash(key)] === undefined) {
return null;
}
return this._items[this._hash(key)];
# The following shows how to pass around methods in Ruby
class ReallyUnboundMethod
def initialize(name, body)
@name, @body = name, body
end
def bind(obj)
metaclass = class << obj; self; end
metaclass.send(:define_method, @name, &@body)
# The following shows how to pass around methods in Ruby
# Unbound methods in Ruby suck, they can only be re-bound to objects that are
# a #kind_of? the original
class ReallyUnboundMethod
def initialize(name, body)
@name, @body = name, body
end
def bind(obj)
require 'rspec'
describe "why freezing constants isn't cargo-culting" do
describe "unfrozen constants" do
THAWED_STRING = "Thawed"
def bar
THAWED_STRING
end