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 looks very nice ;-) also, there are many problems that a genuine singleton object with cloning abilities can illuminate clearly HISTORY 1.0.0 cleanup up a small error where bareword syntax errors in a prototype block were silently ignored 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 #<#:0x10f7fac @port=4242, @host="localhost", @conn_string="localhost:4242"> "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 #<#:0x10fb42c> (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 = Object.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 = Object.prototype{ attributes 'a' => 4, 'b' => 10, 'c' => 2} b = Object.prototype{ a 4; b 10; c 2 } c = Object.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