Install now with sudo gem install classroom
ClassRoom (RubyForge project) is a project to develop a distributed ‘class server’ powered by DRb that I have been working on. Let’s skip long explanations and jump into code. First, we’ll create a very basic “Dog” class with some basic features:
class Dog
attr_accessor :name
def self.count
@@count ||= 0
end
def initialize(options)
self.name = options[:name]
@@count ||= 0
@@count += 1
end
end
Next, we’ll create a program that can use Dog via ClassRoom:
require ‘rubygems’
require ‘classroom’
class_server = ClassRoom::Client.new(‘classroom://:2001′)
class_server.add_class(IO.read(‘dog.rb’))
class_server.load_class(:all)
puts "There are #{Dog.count} dogs"
fido = Dog.new(:name => "Fido")
puts "There are #{Dog.count} dogs"
rufus = Dog.new(:name => "Rufus")
puts "There are #{Dog.count} dogs"
puts "fido’s name is #{fido.name}"
# => There are 0 dogs
# => There are 1 dogs
# => There are 2 dogs
# => fido’s name is Fido
Take care to notice that at no point is dog.rb actually included/’require’d. Read More