Skip to content

Instantly share code, notes, and snippets.

Avatar
💩
俺ら生きてて良いすかお偉いさ〜ん?

Shouichi Kamiya shouichi

💩
俺ら生きてて良いすかお偉いさ〜ん?
View GitHub Profile
View time_scopes.rb
#
# 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
View request_method_overer.rb
# 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)
View daemons.rb
module Daemons
class Base
include SSH
attr_accessor :server
delegate :logger, :address, :to => :server
class << self
def inherited(klass)
View delete_vs_minus.rb
#!/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 Jan 12, 2011
応用統計学・統計解析法レポート問7
View main.R
#!/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)
View ruby_include_idiom.rb
#!/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 Mar 27, 2011
Drawing fractal tree by clojure.
View fractal.clj
; 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)))))]
View maybe.rb
module Maybe
class NullObject
def initialize
@origin = caller.first
end
def __null_origin__
@origin
end
View NutritionFacts.java
/**
* 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;