PPL30 Day 28: DRb
Today… err, yesterday, I began looking at Distributed Ruby, or DRb. DRb is a system for allowing Ruby processes to communicate with each other. Really, it’s a client/server thing.
This is part of the Peer Pressure Learning 30 series. To learn more about what in the hell that means, see the intro to the series, A Good Time To Learn.
Installation
NOTHING! DRb is included in Ruby 1.8+.
Resources
The DRb Site
I started with what I believe to be the DRb site. In particular, the Introduction to DRb was a pretty straightforward way to get started. If your eyes are up to the task of actually reading the content on this site, the content can be quite informative.
What I Think I Now Know
DRb is a lot of fun to play with, for some reason. I don’t really think the DRb site quite does it justice, actually. There are many possibilities. For instance, distributed FizzBuzz!
Server:
#!/usr/bin/env ruby
#
# Server
require 'drb'
class FizzBuzz
def initialize(range = 1..100)
@range = range
end
def range=(home)
@range = home
end
def fizzbuzz
@range.to_a.map do |num|
result = ''
result = 'Fizz' if num % 3 == 0
result += 'Buzz' if num % 5 == 0
result.empty? ? num : result
end
end
end
DRb.start_service 'druby://:9000', FizzBuzz.new
puts DRb.uri
trap("INT") { DRb.stop_service }
DRb.thread.join
Client:
#!/usr/bin/env ruby
#
# Client
require 'drb'
DRb.start_service
fb = DRbObject.new nil, 'druby://:9000'
fb.range = 1..200
fb.fizzbuzz.each {|r| puts r }
Of course we need to access FizzBuzz answers from a remote client, right? Place each of those in an executable file (e.g. the server script in server.rb
, the client script in client.rb
). Then, in one terminal window, run ./server.rb
, and in the other run ./client.rb
. MAGIC!
I really recommend giving the Introduction to DRb a quick skim, at least. It’s really not too complicated, as long as you’re awake enough to read it clearly.
I’m so close. I will be doubling up tomorrow, as tomorrow marks the final day of the PPL30. I swear, I’m going to finish. Success is within my grasp, and I shall not allow accolades of such great import slip feebly from my fingers! … Yup, so, Schema and MacRuby.