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
def tputs(*params) | |
$io_lock ||= Mutex.new | |
$io_lock.synchronize{ puts(*params) } | |
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 ThreadPool | |
def initialize(input_array:, callback:, threads: 128) | |
@callback, @input_array, @threads = callback, input_array, threads | |
end | |
def run | |
results = [] | |
input_queue, results_queue = Queue.new, Queue.new | |
@input_array.each{ |item| input_queue << item } |
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 Hash | |
def stringify_keys | |
result = {} | |
each_key do |key| | |
result[key.to_s] = if self[key].class == Hash | |
self[key].stringify_keys | |
else | |
self[key] | |
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 DoubleUserBalance < ActiveRecord::Migration | |
def up | |
# Double user's balance | |
prepared = ActiveRecord::Base.connection.raw_connection.prepare(" | |
UPDATE users | |
SET balance = balance * ? | |
") | |
prepared.execute(2) | |
prepared.close | |
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 String | |
REPLACE_INVALID_WITH = '�' | |
def to_utf8 | |
self.encode('utf-8', invalid: :replace, undef: :replace, replace: REPLACE_INVALID_WITH). | |
chars.map{ |i| i.valid_encoding? ? i : REPLACE_INVALID_WITH }.join | |
end | |
end |