Will it grind ?
Yes, and in parallel !
codegrinder is a queue associated to one or several thread that share the same logic, and process the elements inserted into it.
How to install ?
gem install codegrinder
How to use ?
First, you have to require codegrinder:
require 'codegrinder'
Next, you have to build your codegrinder:
# That codegrinder print the square of any inserted value:
my_grinder = CodeGrinder.new { |i| print(String(i ** 2) + "\n") }
At last, you can start it:
# Starting the grinder with 4 threads. If no number is given, a single
# thread is used.
my_grinder.start(4)
From now on, any time you will insert a value in your grinder, it will be squared and printed to screen by one of your 4 threads.
Full example:
# Parallelized tiny pry-like implementation
STDOUT.sync = true
STDERR.sync = true
require 'codegrinder'
code_processor = CodeGrinder.new do |code|
begin
STDERR.write " => #{eval(code).inspect}\n"
rescue StandardError => e
STDERR.write " => #{e.class}: #{e.}\n"
end
end
code_processor.start(4)
QUIT = 'quit'
def self.next_command
STDOUT.write("Enter command:\n")
result = gets
result.nil? ? QUIT : result.chomp
end
while (command = next_command) != QUIT
code_processor << command
end
This small ruby shell can process up to 4 commands at the same time.