NAME

   prototype.rb

URIS

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

SYNOPSIS

   prototype.rb facilitates a prototype based coding style

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

   for ruby

WHY

   prototype based programming can look very nice ;-)

SAMPLES

   <========< samples/a.rb >========>

   ~ > 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


   <========< samples/b.rb >========>

   ~ > cat samples/b.rb

     require 'prototype'

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

       conn_string [host, port].join(':')

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

     p DB
     p DB.host
     p DB.port
     p DB.conn_string

     DB.connect

   ~ > ruby samples/b.rb

     #<#<Class:0xb7593c54>:0xb759322c @conn_string="localhost:4242", @port=4242, @host="localhost">
     "localhost"
     4242
     "localhost:4242"
     ["localhost", 4242]


   <========< samples/c.rb >========>

   ~ > cat samples/c.rb

     require 'prototype'

     a = Prototype.new{
       def method() 42 end
     }

     b = a.clone

     p a.method
     p b.method

     a.extend{
       def method2() '42' end
     }

     p a.method2
     p b.method2

     b.extend{
       def method3() 42.0 end
     }

     p b.method3
     p a.method3  # throws error!


   ~ > ruby samples/c.rb

     42
     42
     "42"
     "42"
     42.0
     samples/c.rb:24: undefined method `method3' for #<#<Class:0xb7595790>:0xb7595420> (NoMethodError)


   <========< samples/d.rb >========>

   ~ > cat samples/d.rb

     require 'prototype'

     proto = prototype{ attributes 'a' => 1, 'b' => 2, 'c' => 3 }
     proto = prototype{ a 1; b 2; c 3 }

     %w( a b c ).each{|attr| p proto.send(attr)}

     clone = proto.clone
     proto.c = 42

     %w( a b c ).each{|attr| p proto.send(attr)}
     %w( a b c ).each{|attr| p clone.send(attr)}

   ~ > ruby samples/d.rb

     1
     2
     3
     1
     2
     42
     1
     2
     3


   <========< samples/e.rb >========>

   ~ > cat samples/e.rb

     require 'prototype'

     proto = prototype{
       @a = 40
       @b = 2
     }

     p(proto.a + proto.b)

   ~ > ruby samples/e.rb

     42


   <========< samples/f.rb >========>

   ~ > cat samples/f.rb

     require 'prototype'

     a = prototype{ attributes 'a' => 4, 'b' => 10, 'c' => 2}

     b = prototype{ a 4; b 10; c 2 }

     c = prototype{ @a = 4; @b = 10; @c = 2 }

     [a, b, c].each{|obj| p(obj.a * obj.b + obj.c) }

   ~ > ruby samples/f.rb

     42
     42
     42


   <========< samples/g.rb >========>

   ~ > cat samples/g.rb


     require 'prototype'

     a = prototype

     b = prototype(a){ @a, @b, @c = 4, 10, 2 }

     a.extend{ def answer() a * b + c end }

     p b.answer


   ~ > ruby samples/g.rb

     42


   <========< samples/h.rb >========>

   ~ > cat samples/h.rb

     require 'prototype'

     proto = prototype{
       a 1
       b 1
       c 40

       sum { a + b + c }
     }

     p proto.sum

   ~ > ruby samples/h.rb

     42


   <========< samples/i.rb >========>

   ~ > cat samples/i.rb

     require 'prototype'

     o = Object.new

     o.prototyping do
       @a = 42
       attr 'a'
     end
     p o.a

     o = prototype do
       @a = 42
       attr 'a'
     end
     p o.a

     o.prototyping do
       @b = 42
       attr 'b'
     end
     p o.b
     o.prototyping do
       @b = 42.0
     end
     p o.b

   ~ > ruby samples/i.rb

     42
     42
     42
     42.0


-a
-- 
what science finds to be nonexistent, we must accept as nonexistent; but what
science merely does not find is a completely different matter... it is quite
clear that there are many, many mysterious things.
- h.h. the 14th dalai lama