Skip to content

Instantly share code, notes, and snippets.

View ttanimichi's full-sized avatar

Tsukuru Tanimichi ttanimichi

View GitHub Profile
@ttanimichi
ttanimichi / included_sample.rb
Created September 14, 2014 03:35
モジュールに self.included を定義してインスタンスメソッドとクラスメソッドを同時に Mixin するサンプル
module M
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def b
puts 'b'
end
end
@ttanimichi
ttanimichi / prepended_sample.rb
Created September 14, 2014 07:10
prependされたときにクラスメソッドとインスタンスメソッドを同時に追加するパターン
module M
def self.prepended(base)
class << base
self.prepend(ClassMethods)
end
end
module ClassMethods
def c
puts 'c'
@ttanimichi
ttanimichi / retry_if_collide.rb
Created September 15, 2014 02:11
乱数などを格納する列にUnique制約をかけた場合に、万が一衝突が発生したらリトライするモジュール
# depends on save, save!, create, create!
module RetryIfCollide
extend ActiveSupport::Concern
def save(*)
self.class._retry_if_collide { super }
end
def save!(*)
self.class._retry_if_collide { super }
echo "This script requires superuser access to install rpm packages."
echo "You will be prompted for your password by sudo."
# clear any previous sudo permission
sudo -k
# run inside sudo
sudo sh <<SCRIPT
# add GPG key
require 'test/unit'
class TestSample < Test::Unit::TestCase
def setup
end
def test_sample
coins = [1, 5, 50]
target_coin = 10
assert do
@ttanimichi
ttanimichi / method_chain_stub.rb
Created January 20, 2015 10:18
メソッドチェーンをstubするObjectを返す関数
def method_chain_stub
object = Object.new
object.instance_eval do
self.singleton_class.class_eval do
define_method(:method_missing) { |name| self }
end
end
object
end
@ttanimichi
ttanimichi / dip.rb
Last active August 29, 2015 14:14
Rubyによる依存関係逆転の原則(動的言語版)
# Rubyによる依存関係逆転の原則
class Button
# 規約: 引数`device`は `on?`, `turn_on`, `turn_off`に反応すること
def initialize(device)
@device = device
end
def press
if @device.on?
@ttanimichi
ttanimichi / config.ru
Created February 10, 2015 06:44
A Minimal Rack Application
run ->(env) { [200, { 'Content-Type' => 'text/html' }, ['Hello, World']] }
(defun note ()
(interactive)
(let ((path "~/Dropbox/Notes/text/") (filename (format-time-string "%Y%m%d%H%M%S")))
(if (file-directory-p path) (switch-to-buffer (find-file (concat path filename ".md")))
(message (concat "No such directory: " path)))))
(global-set-key (kbd "C-c m") 'note)
(defun ruby ()
(interactive)
(let ((path "~/Dropbox/Notes/ruby/") (filename (format-time-string "%Y%m%d%H%M%S")))
@ttanimichi
ttanimichi / ruby-mode-ac-dict.md
Last active August 29, 2015 14:16
Ruby および Rails 用の auto-complete.el の辞書。すべての public メソッドを列挙している

Ruby および Rails 用の auto-complete.el の辞書

# Gemfile
source 'https://rubygems.org'

gem 'rails', '4.2.0'
gem 'pg'