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 'open3' | |
Open3.popen3 'bash' do |stdin, stdout, stderr, wait| | |
t=Thread.new{ stdout.each{|l| print l } } | |
stdin.puts 'ls' | |
stdin.puts 'exit' | |
wait.join | |
t.join | |
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 Waiter | |
def initialize | |
@m, @cv = Mutex.new, ConditionVariable.new | |
@signalled = false | |
end | |
def wait | |
@m.synchronize{ @cv.wait(@m) until @signalled; @signalled = false } | |
end | |
def signal | |
@m.synchronize{ @signalled = true; @cv.signal } |
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 Fluent | |
class TextParser | |
class StringifiedValuesJSONParser < JSONParser | |
Plugin.register_parser("stringified_values_json", self) | |
def parse(text) | |
time, record = super(text) | |
# replace all Hash (like a dict) values with stringified JSON | |
record = record.inject({}) { | new_hash, (key, value) | |
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 'socket' | |
class Page | |
attr_reader :body, :method, :path | |
def initialize(m, p, b) | |
@body, @method, @path = b, m, p | |
end | |
end | |
class Server |
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 'socket' | |
server1 = TCPServer.new('0.0.0.0', 1337) | |
server2 = TCPServer.new('0.0.0.0', 1338) | |
servers = [server1, server2] | |
clients = [] | |
loop do | |
readables, * = IO.select(servers+clients) |
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
PIPELINE = %w[booked collected ironed delivered] | |
def state | |
PIPELINE[@pipeline_position] | |
end | |
def advance | |
@pipeline_position += 1 | |
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 'thread' | |
class WorkerPool | |
def initialize | |
@num = 10 | |
@jobs = Queue.new | |
@results = Queue.new | |
@stopped = false |
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 'socket' | |
def log(s) | |
puts "#{Time.now.strftime('%H:%M:%S')}: #{s}" | |
end | |
server = TCPServer.new('', 1337) | |
loop do | |
socket = server.accept |
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 'socket' | |
def log(s) | |
puts "#{Time.now.strftime('%H:%M:%S')}: #{s}" | |
end | |
html = "<a href=\"/foo\">Link</a>" | |
server = TCPServer.new('', 1337) |
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' | |
state = {time: Time.now, status: "Flux Capacitor running"} | |
messages = Queue.new | |
class TimeChange | |
attr_reader :time | |
def initialize(t) | |
@time = t | |
end |