# Prototype-0.0.0

**URL:** <https://www.ruby-forum.com/t/prototype-0-0-0/98126>\
**Category:** Ruby\
**Created:** [July 12, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/prototype-0-0-0/98126 "2006-07-12T17:42:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 12, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/prototype-0-0-0/98126/1 "2006-07-12T17:42:15Z")

</div>

NAME

prototype.rb

URIS

[http://codeforpeople.com/lib/ruby/](http://codeforpeople.com/lib/ruby/)  
[http://rubyforge.org/projects/codeforpeople/](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 😉

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 12, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/prototype-0-0-0/98126/2 "2006-07-12T22:05:56Z")

</div>

removed\_email\_address@domain.invalid 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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 12, 2006, 10:09pm UTC](https://www.ruby-forum.com/t/prototype-0-0-0/98126/3 "2006-07-12T22:09:15Z")

</div>

On Thu, 13 Jul 2006, Pit C. wrote:

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

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 12, 2006, 10:18pm UTC](https://www.ruby-forum.com/t/prototype-0-0-0/98126/4 "2006-07-12T22:18:46Z")

</div>

removed\_email\_address@domain.invalid 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
