This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Validatable | |
def self.included(base) | |
base.extend ClassMethods | |
end | |
module ClassMethods | |
def validate(attribute, &block) | |
attr_accessor attribute unless defined?(attribute) | |
alias_method "#{attribute}_orig=", "#{attribute}=" | |
define_method("#{attribute}=") do |val| | |
self.__send__("#{attribute}_orig=", val) if block.(val) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Delegatable | |
def self.included(base) | |
base.class_eval do | |
def self.delegate_method(method, to) | |
define_method(method) do |*args| | |
instance_variable_get(to).__send__(method, *args) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Gmail | |
def initialize(&block) | |
@block = block | |
@items = [] | |
end | |
def run | |
self.instance_eval(&@block) | |
@items.join(" ") | |
end | |
def cond(condition, &block) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 問題: | |
# http://hidesugar.web.fc2.com/ym2/ym2musikuizan2.txt | |
# ABCDEF * G = FABCDE | |
# 解答: 230769 * 4 = 923076 | |
def solve(sx, sy, sz, mapping) | |
x = sx.inject(0) {|sum, x| sum * 10 + mapping[x]} | |
y = sy.inject(0) {|sum, y| sum * 10 + mapping[y]} | |
z = Enumerator.new do |e| | |
t = (x * y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Future | |
def initialize(&block) | |
@que = Queue.new | |
@t = Thread.new { | |
@que << block.call | |
} | |
end | |
def value | |
return @que.pop | |
ensure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ConnectionPool | |
attr_reader :conns | |
def initialize(max, &block) | |
@mutex = Mutex.new # マルチスレッドからの排他的ロック | |
@cv = ConditionVariable.new | |
@max = max | |
@que = [] | |
@conns = [] | |
@create_block = block | |
@created = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/md5' | |
module SecurablePassword | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
def has_secure_password(name = :password) | |
include HasSecurePassword.new(name) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Topic | |
attr_reader :name | |
def initialize(name) | |
@name = name | |
@clients = {} | |
end | |
def publish(message) | |
@clients.values.each {|client| client.receive(@name, message)} | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'open_uri_redirections' |