Skip to content

Instantly share code, notes, and snippets.

制約フレームワークを書かないと連載させてもらえない Web 雑誌があるようなので、昨日こんなの書いてた。 静的解析もできないし、ドキュメンテーションにも活きないので各方面の識者から怒られそうだが {} で書けるという見た目の点で許してほしい。

あとはつっこまれるとしたら、継承したときに事前条件、事後条件が強化される/緩和されるのあたり。全部やると大変なんすよ。invariant もないですね (method_added でイベントフックしてすべてのメソッドコールへ prepend かぶせるとかすればできるかな)。

class BeerLover
  extend Contracts
  attr_accessor :age
#!/usr/bin/env ruby
#
# `git pr-get 37` => Fetch Pull Resuest #37 and create branch `pr-37`
#
# Requirements:
# Ruby
#
# Install:
# $ chmod +x git-pr-get
# $ mv git-pr-get (somewhere listed in PATH)
require 'tokyotyrant'
module Rack
module Session
class Tyrant < Abstract::ID
include TokyoTyrant
attr_reader :mutex, :pool
DEFAULT_OPTIONS = Abstract::ID::DEFAULT_OPTIONS.merge :tyrant_server => "localhost:1978"
def initialize(app, options = {})
super
module Rack
class MemoryBloat
def initialize(app, logger)
@app = app
@logger = logger
end
def call(env)
memory_usage_before = memory_usage
result = @app.call(env)
module Rack
# Renders a valid robots.txt according to http://www.robotstxt.org
# This is an example of over-engineering. But it's simple example of
# how you might test your middleware.
class RobotsTxt
def initialize(app)
@app = app
end
def call(env)
# Pattern matcher
# Usage
# When pattern matched, block is yielded with argument is hash that maps
# symbol to value.
#
# mat = Matcher.new
# mat.pattern([:a, :b]) {|hash|
# p hash
# }
# mat.match([1, 2])
# -*- coding: utf-8 -*-
module SelectTagHelper
##
## Generate <select> tag. Both <option> and <opttags> are supported.
##
## ex.
## <% values = {'H'=>'Haruhi', 'M'=>'Mikuru', 'Y'=>'Yuki'} %>
;; htmlutil - Some useful tools for working with HTML text and HTTP URLs
; This module provides tools for escaping HTML text, with HTML
; entities for special characters; and for escaping strings to be
; safely included into URLS; and back.
;; Primary commands:
; html-escape-str - Escape (quote) a character for HTML
; html-unescape-str - Un-escape an HTML entity string
; url-quote-str - Quote special characters in a URL string
if Rails.version == '2.3.8' && Gem.available?('mongrel', Gem::Requirement.new('~>1.1.5')) && self.class.const_defined?(:Mongrel)
# Pulled right from latest rack. Old looked like this in 1.1.0 version.
#
# def [](k)
# super(@names[k] ||= @names[k.downcase])
# end
#
module Rack
# Unused Variable Checker
#
# Usage: ruby varcheck.rb test-src.rb
#
#
module VarCheck
class InstSeqTree
Headers = %w(magic major_version minor_version format_type
misc name filename filepath line type locals args
exception_table)