[ANN] prototype-0.0.0

NAME

   prototype.rb

URIS

   http://codeforpeople.com/lib/ruby/
   http://rubyforge.org/projects/codeforpeople/

SYNOPSIS

   prototype.rb implements the prototype design pattern

     http://en.wikipedia.org/wiki/Prototype-based_programming

   for ruby

WHY

   prototype based programming can look very nice :wink:

EXAMPLES

   ~ > cat samples/a.rb
   require 'prototype'

   singleton = Prototype.new{
     @a, @b = 40, 2

     def answer() @a + @b end
   }

   p singleton.answer

   ~ > ruby samples/a.rb
   42

   ~ > cat samples/b.rb
   require 'prototype'

   DB = Prototype.new{
     host 'localhost'
     port 4242

     def connect() p [host, port] end
   }

   p DB.host
   p DB.port
   DB.connect

   ~ > ruby samples/b.rb
   "localhost"
   4242
   ["localhost", 4242]

DOCS

   see

     lib/*rb
     samples/*rb

comments and patches welcome!

enjoy!

-a

···

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

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

<snip - hey no fair stealing my 0.1.0 release!>

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

cheers.

-a

···

On Thu, 13 Jul 2006, Pit Capitain wrote:

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

--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

ara.t.howard@noaa.gov schrieb:

<snip - hey no fair stealing my 0.1.0 release!>

So sorry :wink:

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