Skip to content

Instantly share code, notes, and snippets.

View shouichi's full-sized avatar

Shouichi Kamiya shouichi

  • Tokyo
  • 16:09 (UTC +09:00)
View GitHub Profile
#
# This module is a collection of named_scope for time column.
# Requires MySQL to work because of usage of MySQL functions.
# But you can easly replace that part to work with another databases.
#
# Example
#
# class Article < ActiveRecord::Base
# include TimeScopes[:created_at]
# end
# This rack app overrides http method by __method=xxx query.
#
# Example
#
# link_to 'Destroy', post_path(@post, :__method => :delete)
class RequestMethodOverrider
HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
HTTP_METHOD_OVERRIDE_KEY = "__method".freeze
def initialize(app)
module Daemons
class Base
include SSH
attr_accessor :server
delegate :logger, :address, :to => :server
class << self
def inherited(klass)
#!/usr/bin/env ruby
a = [1,2,3,4]
a.each { |x| a.delete(x) }
#=> [2,4]
a = [1,2,3,4]
a.each { |x| a -= [x] }
#=> []
@shouichi
shouichi / main.R
Created January 12, 2011 17:33
応用統計学・統計解析法レポート問7
#!/usr/bin/env Rscript
# 統計データがなかったので、お正月番組に触発されてテーブルクロス引きでコップが
# ずれた距離を測りました。コップの中の水の容量は
# (x1, x2,..., x5) = (0, 50,.., 200)
# です。それぞれの容量で5回ずつ測定。単位はcmとccです。
x1 <- c(1.5, 2.2, 2.4, 2.2, 2.7)
x2 <- c(1.8, 1.6, 1.9, 1.6, 1.6)
x3 <- c(1.7, 2.1, 1.6, 1.5, 2.0)
#!/usr/bin/env ruby
module Teacher
def self.included(model)
model.instance_eval do
extend ClassMethods
include InstanceMethods
end
end
@shouichi
shouichi / fractal.clj
Created March 27, 2011 09:44
Drawing fractal tree by clojure.
; This is from http://www.learningclojure.com/2009/09/fractal-tree.html
(import '(javax.swing JFrame JPanel )
'(java.awt Color Graphics Graphics2D))
(defn draw-tree [#^Graphics g2d angle x y length branch-angle depth]
(if (> depth 0)
(let [new-x (- x (* length (Math/sin (Math/toRadians angle))))
new-y (- y (* length (Math/cos (Math/toRadians angle))))
new-length (fn [] (* length (+ 0.75 (rand 0.1))))
new-angle (fn [op] (op angle (* branch-angle (+ 0.95 (rand)))))]
module Maybe
class NullObject
def initialize
@origin = caller.first
end
def __null_origin__
@origin
end
/**
* Builder pattern example from Effective Java.
*/
public final class NutritionFacts {
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
*.png
*.eps