Joe Van Dyk wrote:
> On 8/18/05, Jamis Buck <jamis / 37signals.com> wrote:
>> On Aug 18, 2005, at 3:40 PM, Joe Van Dyk wrote:
>> 
>>> I'm writing an application that controls a group/cluster of linux
>>> computers/nodes.  I need to have a configuration file that lists the
>>> nodes in the cluster.
>>> 
>>> It would be neat if the configuration file was in Ruby.  If I had
>>> nodes named node1 and node3, the configuration file could look a
>>> little like:
>>> 
>>> node :node1 do
>>>   ip 192.whatever
>>>   title "Node 1"
>>> end
>>> 
>>> node :node2 do
>>>   ip 192.whatever
>>>   title "Node 2"
>>> end
>>> 
>>> So, is there some standard Ruby idiom for how to read a file and
>>> execute the code?  Just load the file and instance_eval it?
>> 
>> That's what SwitchTower does. I'm sure there's many other ways to do
>> it, but it works well enough.
> 
> Thanks!  I'll check it out.
> 
> So, say I have the following class
> 
> class ClusterManager
> 
>   def load_config_file config_file
>     instance_eval File.read(config_file)
>   end
> 
>   def node node_id, &block
>     # What goes here?
>   end
> 
> end
> 
> Or should I set it up differently?

Here's how I'd do it;

class ClusterManager

  def self.load_config_file config_file
    cm = new
    cm.instance_eval File.read(config_file)
    cm
  end

  def initialize ...
  # and other CM methods

  def node node_id, &block
    # What goes here?
  end

  class Node
    ...
  end
end

Then you can do

cm = ClusterManager.load_config_file "cluster.conf"

Instead of class Node you could also use Hashes.  Then you can have

node(
  :name => "foo",
  :size => 10,
  ...
)

Kind regards

    robert