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:
my_grinder = CodeGrinder.new { |i| print(String(i ** 2) + "\n") }
At last, you can start it:
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:
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.message}\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.