Skip to content

Instantly share code, notes, and snippets.

# You write a class called Wrapper, that has a single static function named
# wrap that takes two arguments, a string, and a column number. The function
# returns the string, but with line breaks inserted at just the right places
# to make sure that no line is longer than the column number. You try to break
# lines at word boundaries.
#
# Like a word processor, break the line by replacing the last space in a line
# with a newline.
class Wrapper
#!/bin/sh
set -e
# Add host-only network
cat <<-EOF > /etc/network/interfaces
# Loopback
auto lo
iface lo inet loopback
#!/bin/sh
set -e
# Add host-only network
cat <<-EOF > /etc/network/interfaces
# Loopback
auto lo
iface lo inet loopback
require 'rspec'
describe "why freezing constants isn't cargo-culting" do
describe "unfrozen constants" do
THAWED_STRING = "Thawed"
def bar
THAWED_STRING
end
# 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)
# 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)
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)];
require 'rspec'
module Evil
def defn(method_name)
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method_name}
end
METHOD
end
end
module Formattable
def format(opts)
# ...
end
end
class Basketball::PlayerRecord
extend Formattable
format :fields =>[:rebounds_total, :minutes, :assists, :points], :with => '-'
end
def digits(string)
string.each_car.select { |character| /\d/.match(character).present? }
end