On Nov 8, 2007, at 8:14 PM, Pe, Botp wrote:

> From: ara.t.howard [mailto:ara.t.howard / gmail.com]
> # class Config
>
> i get a TypeError here :)
>
> #    attribute "width"
> #    attribute "height"
> #    attribute "color"
>
> is it possible without declaring those above?
> i mean, something like some sort of attrib_eval(like below) but  
> automagically creates those attributes...
>
> #      config.instance_eval IO.read(path)
>
> kind regards -botp
>

if i'm following correctly, you mean this?


cfp:~ > cat a.rb
require "attributes"

class Config
   def self.load path
     config = new
     config.configure IO.read(path)
     config
   end

   def configure *a, &b
     @configuring = true
     instance_eval *a, &b
   ensure
     @configuring = false
   end

   def method_missing m, *a, &b
     super unless @configuring
     attribute m
     send m, *a, &b
   end
end

config = Config.load "b.rb"

p config

config.configure{ width 42.0 }

p config


cfp:~ > cat b.rb
configure {
   width 42

   height 42.0

   color "pinky-beige"

   honky true
}



cfp:~ > ruby a.rb
#<Config:0x21020 @width=42, @height=42.0, @configuring=false,  
@color="pinky-beige", @honky=true>
#<Config:0x21020 @width=42.0, @height=42.0, @configuring=false,  color="pinky-beige", @honky=true>

??


a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama