prototype design pattern

T

transfire

What criteria for "good"? I'm not aware of any lib that preports
"enterprise quality" or anthing like that. I've played with the ideas
though. Prototype-based OOP interests me. On the surface it appears as
simple as aliasing an Object#new to #$dup and instantiation a bunch of
initiaial prototypes for each class -- of course some classes won't
play nice woth this, like File for instance.

Other than that I also played around with this:

module ProtobaseKernel

def meta ; (class << self; self; end) ; end

def obj(&blk)
o = Object.new
o.instance_eval(&blk)
h = {}
iv = o.instance_variables
iv.each { |k| h[k[1..-1].to_sym] = o.instance_eval{
instance_variable_get(k) } }
o.meta.class_eval {
h.each { |k,v|
case v
when Proc
#define_method(k){ |*args| v[*args] }
attr_reader k
else
attr_accessor k
end
}
}
o
end

def fn(&blk)
proc(&blk)
end

def new(o=nil)
return o.clone if o
return self.clone
end

end

class Object
include ProtobaseKernel
end

# Example -----------------

person = obj {
@name = ''
@age = 0
@announce = fn { |x| "#{x}, #{name} is #{age}" }
}

person.name = 'Tom'
person.age = 35
puts person.announce['Peter']
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,208
Messages
2,571,082
Members
47,683
Latest member
AustinFairchild

Latest Threads

Top