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
# 問題: | |
# 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
# 問題: https://twitter.com/nada_mathclub/status/1256418924737880069 | |
# : vivid * vive = brillante の覆面算を解く | |
# 解答: 62621 * 6267 = 392445807 | |
def solve(v, i, d, e) | |
return nil if v == 0 | |
vivid = [v, i, v, i, d].inject(0) {|sum, x| sum * 10 + x} | |
vive = [v, i, v, e].inject(0) {|sum, x| sum * 10 + x} | |
brillante = Enumerator.new do |e| | |
t = (vivid * vive) |
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 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' |
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 'thread' | |
class Workers | |
def initialize(count) | |
@q = Queue.new | |
@count = count | |
@jobs = (0...@count).map.with_index {|id| | |
Thread.new do | |
while (job = @q.pop) | |
total = job.(id, total) |
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 TimeoutError < StandardError; end | |
def timeout(sec) | |
x = Thread.current | |
t = Thread.new do | |
begin | |
sleep sec | |
x.raise TimeoutError.new "Exceed maximum timeout value #{sec}s" | |
rescue e | |
x.raise e | |
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
require 'strscan' | |
BEGIN_TAGS = %w(<%= <%# <%) | |
END_TAGS = %w(%>) | |
class MiniErb | |
def scan(content) | |
scanner = ::StringScanner.new(content) | |
state = :text | |
until scanner.eos? | |
case state |
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 Forwardable | |
def delegate_methods(obj, *methods) | |
methods.each {|method| delegate_method(obj, method)} | |
end | |
def delegate_method1(obj, method) | |
self.module_eval %Q{ | |
def #{method}(*args, &block) | |
_#{obj}.#{method}(*args, &block) | |
end | |
} |