This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def digits(string) | |
string.each_car.select { |character| /\d/.match(character).present? } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Formattable | |
def format(opts) | |
# ... | |
end | |
end | |
class Basketball::PlayerRecord | |
extend Formattable | |
format :fields =>[:rebounds_total, :minutes, :assists, :points], :with => '-' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
module Evil | |
def defn(method_name) | |
class_eval <<-METHOD, __FILE__, __LINE__ + 1 | |
def #{method_name} | |
end | |
METHOD | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
describe "why freezing constants isn't cargo-culting" do | |
describe "unfrozen constants" do | |
THAWED_STRING = "Thawed" | |
def bar | |
THAWED_STRING | |
end |