Skip to content

Instantly share code, notes, and snippets.

@kokudori
kokudori / gist:1253650
Created September 30, 2011 12:51
特異クラスについて
module Piyo
def piyo_func
'piyo_func'
end
end
class Hoge
include Piyo
def initialize
class << self
@kokudori
kokudori / gist:1290749
Created October 16, 2011 10:38
モンテカルロ法で円周率計算
def monte n #点を打つ数
r = 0
n.times do
# PIの確率でHITするから円内にHITすれば+1
r+=1 if Math.sqrt(rand()**2+rand()**2) < 1
end
# 円の面積/正方形の面積がPI/4
4.0*r / n
end
@kokudori
kokudori / gist:1303589
Created October 21, 2011 11:20
CoffeeScriptでFibBuzz
for fib in [1..10].map((x) -> do (x) -> if x is 0 or x is 1 then x else arguments.callee(x-1)+arguments.callee(x-2))
console.log do -> if fib % 15 is 0 then 'FizzBuzz' else if fib % 3 is 0 then 'Fizz' else if fib % 5 is 0 then 'Buzz' else fib
###
実行結果
1
1
2
Fizz
@kokudori
kokudori / gist:1303629
Created October 21, 2011 11:37
CoffeeScriptでBuzzFib
fib = [1..10].map((x) -> do (x) -> if x is 0 or x is 1 then x else arguments.callee(x-1)+arguments.callee(x-2))
for n in [1..10]
console.log if n in fib and n % 5 is 0 then 'FibBuzz' else if n in fib then 'Fib' else if n % 5 is 0 then 'Buzz' else n
###
実行結果
Fib
Fib
Fib
4
@kokudori
kokudori / gist:1303644
Created October 21, 2011 11:50
CoffeeScriptでFizzBuzz
for n in [1..100]
console.log switch
when n % 15 is 0
'FizzBuzz'
when n % 5 is 0
'Buzz'
when n % 3 is 0
'Fizz'
else
n
@kokudori
kokudori / gist:1343329
Created November 6, 2011 19:12
DateFormat.jsをCoffeeScriptで書いてみた。
###
DateFormat.jsは
http://yas-hummingbird.blogspot.com/2010/03/javascript-netdate.html
さんが書かれたJSのDateのtoString用パッチです。
ライセンスは下記を参照ください。
###
###
Javascript Date dateFormat
Copyright 2010
@kokudori
kokudori / gist:1362065
Created November 13, 2011 12:37
Beautiful Codeの正規表現マッチャのRuby移植版
class MyRegexp
def self.match(regexp, text)
if regexp[0] == '^'
return matchhere(regexp[1,regexp.size], text)
end
text.chars.with_index do |c,i|
return true if matchhere(regexp, text[i,text.size])
end
return false
end
@kokudori
kokudori / gist:1366106
Created November 15, 2011 04:10
Rubyでニコ生のコメントを取得
# -*- encoding: utf-8 -*-
require 'net/http'
require 'rubygems'
require 'nokogiri'
class Comment
class << self
attr_accessor :cookie
end
#下の値はブラウザのクッキーから取得してください
@kokudori
kokudori / gist:1369625
Created November 16, 2011 08:58
jQueryのget
get: function( num ) {
return num == null ?
// Return a 'clean' array
this.toArray() :
// Return just the object
( num < 0 ? this[ this.length + num ] : this[ num ] );
}
@kokudori
kokudori / gist:1375570
Created November 18, 2011 04:05
JavaScriptのプロパティ解決の擬似コード
Object.prototype.send = function (name) {
return (function (prototype) {
if (!prototype)
return undefined;
if (name in prototype)
return prototype[name];
arguments.callee(prototype.__proto__);
})(this);
};