Skip to content

Instantly share code, notes, and snippets.

Avatar
🚴‍♂️

takuma-saito

🚴‍♂️
View GitHub Profile
View future.rb
class Future
def initialize(&block)
@que = Queue.new
@t = Thread.new {
@que << block.call
}
end
def value
return @que.pop
ensure
View connection_pool.rb
class ConnectionPool
attr_reader :conns
def initialize(max, &block)
@mutex = Mutex.new # マルチスレッドからの排他的ロック
@cv = ConditionVariable.new
@max = max
@que = []
@conns = []
@create_block = block
@created = 0
@takuma-saito
takuma-saito / has_secure_password.rb
Last active May 3, 2020
has_secure_password.rb
View has_secure_password.rb
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
View pubsub.rb
class Topic
attr_reader :name
def initialize(name)
@name = name
@clients = {}
end
def publish(message)
@clients.values.each {|client| client.receive(@name, message)}
end
@takuma-saito
takuma-saito / Gemfile
Last active May 2, 2020
worker_get_url.rb
View Gemfile
source 'https://rubygems.org'
gem 'open_uri_redirections'
View worker.rb
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)
View timeout.rb
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
View mini_erb.rb
require 'strscan'
BEGIN_TAGS = %w(<%= <%# <%)
END_TAGS = %w(%>)
class MiniErb
def scan(content)
scanner = ::StringScanner.new(content)
state = :text
until scanner.eos?
case state
View forwardable.rb
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
}
View fukumen.rb
# 問題: 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)
You can’t perform that action at this time.