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
| # 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 |
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
| #!/bin/sh | |
| set -e | |
| # Add host-only network | |
| cat <<-EOF > /etc/network/interfaces | |
| # Loopback | |
| auto lo | |
| iface lo inet loopback |
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
| #!/bin/sh | |
| set -e | |
| # Add host-only network | |
| cat <<-EOF > /etc/network/interfaces | |
| # Loopback | |
| auto lo | |
| iface lo inet loopback |
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 |
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
| # 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
| 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
| 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
| 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
| def digits(string) | |
| string.each_car.select { |character| /\d/.match(character).present? } | |
| end |