[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[ANN] prototype-0.0.0

Ara.T.Howard

7/12/2006 3:40:00 PM

3 Answers

Pit Capitain

7/12/2006 8:04:00 PM

0

ara.t.howard@noaa.gov schrieb:
>
> NAME
>
> prototype.rb
>
> (...)
>
> comments and patches welcome!

Great idea, and a nice syntax, Ara. Here's another implementation that isn't as
efficient, but more "dynamic" than yours:

module Prototype

def self.new parent = nil, &blk
mod = Module.new do

include parent if parent
extend self

def clone
Prototype.new self
end

def method_missing name, *args
ivar = "@#{name}"
case args.size
when 0
obj = ancestors.find { |m| m.instance_variables.include? ivar }
if obj
obj.instance_variable_get ivar
else
super
end
when 1
instance_variable_set ivar, args.first
else
super
end
end

def do &blk
module_eval( &blk ) if blk
end

end
mod.do( &blk )
mod
end

end

It allows things like:

Obj = Prototype.new {
v 42
def show; p v end
}
Obj.show # => 42

Cld = Obj.clone
Cld.show # => 42

Cld.v 45
Cld.show # => 45
Obj.show # => 42

Obj.do {
x "x"
def m; p x; end
}
Obj.m # => "x"
Cld.m # => "x"

Regards,
Pit

Ara.T.Howard

7/12/2006 8:08:00 PM

0

Pit Capitain

7/12/2006 8:16:00 PM

0

ara.t.howard@noaa.gov schrieb:
> <snip - hey no fair stealing my 0.1.0 release!>

So sorry ;-)

> wait a few minutes and i'll have what i was doing finished - it's basically
> the same thing...

Well, it has to wait until tomorrow, I'm going to bed now. Good to know there'll
be something interesting to read for breakfast...

Regards,
Pit