Skip to content

Instantly share code, notes, and snippets.

@quanon
quanon / konami.js.coffee
Last active August 29, 2015 14:04
コナミコマンド
# normal with Lo-Dash
konami = (handler) ->
keys = []
command = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65]
$(document).on "keydown", (event) ->
keys.push event.which
keys.shift() if keys.length > command.length
@quanon
quanon / rename.rb
Last active August 29, 2015 14:05
ファイルをリネームするスクリプト
# Usage
# ruby rename.rb ~/Downloads/reg_test "homu_(\d+)_homu.(txt)" "sayaka_@1_sayana.@2.backup"
require "fileutils"
dir = File.expand_path(ARGV[0])
pattern = /#{ARGV[1]}$/
after = ARGV[2]
Dir.glob(File.join(dir, "**/*")).each do |filename|
@quanon
quanon / settings.yml
Created October 1, 2014 13:51
Lodge に記事 or コメントの投稿があったら Slack で通知する
lodge:
per_size: 30
right_list_size: 10
slack:
defaults: &defaults
url: https://slack.com
channel: #channel
# トークンは Slack API のページで発行できます。
token: my_token
@quanon
quanon / slack.rb
Last active August 29, 2015 14:07
notice の実装は親クラスに持たせたいけど、実際に notice が呼ばれた際にはサブクラスのインスタンスに委譲したいときはどうしたらいいの?いちいちメソッド名を分けたくないんだよ。
class Slack
def notice
# 処理はサブクラスのインスタンスに委譲したい。
my_child._notice
end
protected
# とりあえずメソッド名の先頭に _ を付けてごまかしている。
@quanon
quanon / iizuka_weather.rb
Last active August 29, 2015 14:12
manabot-iizuka_weather.gemspec
require 'uri'
require 'net/http'
require 'json'
module Ruboty
module Handlers
class IizukaWeather < Base
CITY_CODE = 400030
on /飯塚.+天気\z/, name: 'weather', description: 'Return weather in Iizuka'
@quanon
quanon / benchmark_map_and_flatten.rb
Created January 14, 2015 15:06
flat_map はやい!
require 'benchmark'
array = (1..10_000).map { |n| [n] }
Benchmark.bm do |x|
x.report('map & flatten:') { array.map { |a| a << 'yuno' }.flatten }
x.report('map & flatten!:') { array.map { |a| a << 'yuno' }.flatten! }
x.report('flat_map:') { array.flat_map { |a| a << 'yuno' } }
end
@quanon
quanon / yml_to_struct.rb
Created January 29, 2015 06:41
YAML.load した結果に含まれる Hash を Struct に変換する。
# 使い方:
# yml = File.read(path)
# yml_to_struct(yml)
def yml_to_struct(yml)
yml_object = YAML.load(ERB.new(yml).result)
yml_object_to_struct(yml_object)
end
# YAML.load した結果の Object に含まれる Hash を Struct に変換し、
@quanon
quanon / girl_using_good_name.rb
Last active August 29, 2015 14:19
Module#refine メモ
module GoodName
refine(String) do
def good_name?
true
end
end
def bad_name?
false
end
@quanon
quanon / question.md
Last active August 29, 2015 14:19
集合から要素を n 回取り出した際に重複あり/重複なしがちょうど半々となるには?

問題

x 面のダイスがある。それを n 回振り、ダイスの数ごとに出た回数をカウントする。
この時、出た回数が 1 回の目の種類と 2 回以上の目の種類の比率が
1:1 となるには x は何である必要があるか。n を使って表せ。

出た回数が 1 回の目の数、2 回以上の目の数の比率が 1:1 となるってどういうこと?

x = 12 (12 面ダイス)
n = 10 (10 回振った)

@quanon
quanon / jmotto.js
Last active August 29, 2015 14:20
j-motto のタイムカードに就業時間の列を追加するための user script
// ==UserScript==
// @name j-motto Userscript
// @namespace quanon
// @version 0.1.0
// @author QUANON
// @include https://gws45.j-motto.co.jp/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// @grant none
// ==/UserScript==