# Re: prototype design pattern

**URL:** <https://www.ruby-forum.com/t/re-prototype-design-pattern/98110>\
**Category:** Ruby\
**Created:** [July 12, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110 "2006-07-12T16:46:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 12, 2006, 4:46pm UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110/1 "2006-07-12T16:46:06Z")

</div>

> regards.
> 
> -a

Hm, it’s not at  
[http://wiki.rubygarden.org/Ruby/page/show/ExampleDesignPatternsInRuby](http://wiki.rubygarden.org/Ruby/page/show/ExampleDesignPatternsInRuby)

Anyway, it looks to me like some combination of .clone plus a factory.  
I’m not sure there’s any real savings in Ruby to doing things this way,  
but I’d be happy to be proved wrong.

Regards,

Dan

This communication is the property of Qwest and may contain confidential  
or  
privileged information. Unauthorized use of this communication is  
strictly  
prohibited and may be unlawful. If you have received this communication  
in error, please immediately notify the sender by reply e-mail and  
destroy  
all copies of the communication and any attachments.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 12, 2006, 5:09pm UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110/2 "2006-07-12T17:09:09Z")

</div>

On Wed, 12 Jul 2006, Berger, Daniel wrote:

> Hm, it’s not at  
> [http://wiki.rubygarden.org/Ruby/page/show/ExampleDesignPatternsInRuby](http://wiki.rubygarden.org/Ruby/page/show/ExampleDesignPatternsInRuby)

> **[Prototype-based programming](https://en.wikipedia.org/wiki/Prototype-based_programming)**
>
> Prototype-based programming is a style of object-oriented programming in which behaviour reuse (known as inheritance) is performed via a process of reusing existing objects that serve as prototypes. This model can also be known as prototypal, prototype-oriented, classless, or instance-based programming.
> Prototype-based programming uses the process generalized objects, which can then be cloned and extended. Using fruit as an example, a "fruit" object would represent the properties and functionali...

> Anyway, it looks to me like some combination of .clone plus a factory. I’m  
> not sure there’s any real savings in Ruby to doing things this way, but I’d  
> be happy to be proved wrong.

hi daniel-

savings is only keystrokes. but it can very elegant looking and, these  
days,  
ruby coding bores me unless it looks good too - solving problems is just  
too  
easy in it to provide enough challenge! 😉 take this code for  
example:

```
 fortytwo :~/eg/ruby/prototype > cat a.rb
 require 'prototype'

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

   def answer() @a + @b end
 }

 p singleton.answer

 fortytwo :~/eg/ruby/prototype > ruby a.rb
 42

 fortytwo :~/eg/ruby/prototype > cat b.rb
 require 'prototype'

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

   def connect() 'do something' end

   def inspect() [host, port].join ':' end
 }

 p DB
 p DB.host
 p DB.port

 fortytwo :~/eg/ruby/prototype > ruby b.rb
 localhost:4242
 "localhost"
 4242

```

do you like the look of it?

i’ll post my impl in a follow-up.

cheers.

-a

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 13, 2006, 4:40am UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110/3 "2006-07-13T04:40:16Z")

</div>

removed\_email\_address@domain.invalid wrote:

> ```
> DB = Prototype.new{
> host 'localhost'
> port 4242
> 
> def connect() 'do something' end
> 
> def inspect() [host, port].join ':' end
> }
> 
> ```

Why no go the extra step?

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

   connect does{ 'do something' }

   inspect does{ [host, port].join ':' }
 }

```

T.

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 13, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110/4 "2006-07-13T16:29:32Z")

</div>

On Thu, 13 Jul 2006 removed\_email\_address@domain.invalid wrote:

> T.

easy enough, i’d simply make does() return a block extended with a  
marker:

def does &b  
class \<\< b  
def does() true end  
end  
b  
end

then, later

if b.respond\_to? ‘does’  
# defined a method  
else  
# attribute with proc value  
end

otherwise i could tell the difference between

foobar lambda{ ‘plain ol block’ }

foobar does{ ‘method body’ }

in any case, i’m unclear as to what

connect does{ ‘do something’ }

buys you over

def connect() ‘do something’ end

other than two chars. i admit it looks nice though… actually i guess  
the  
closure might make it nice since you could do

a = 42

connect does{ p a }

which you cannot with a normal method def. so - what’s **your**  
reasoning on  
this?

cheers.

-a

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 13, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110/5 "2006-07-13T21:30:29Z")

</div>

removed\_email\_address@domain.invalid writes:

> Why no go the extra step?
> 
> ```
> DB = Prototype.new{
> host 'localhost'
> port 4242
> 
> connect does{ 'do something' }
> 
> inspect does{ [host, port].join ':' }
> }
> 
> ```

Why not even this:

```
    connect { 'do something' }

    inspect { [host, port].join ':' }
```

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 13, 2006, 10:06pm UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110/6 "2006-07-13T22:06:35Z")

</div>

On Fri, 14 Jul 2006, Christian N. wrote:

> Why not even this:
> 
> ```
> connect { 'do something' }
> 
> inspect { [host, port].join ':' }
> 
> ```

sold.

jib:~/eg/ruby/prototype/prototype-0.2.0 \> cat a.rb

require ‘prototype’

prototype{ connect { p ‘connect’ } }.connect

jib:~/eg/ruby/prototype/prototype-0.2.0 \> ruby -Ilib a.rb

“connect”

-a

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [July 14, 2006, 3:19am UTC](https://www.ruby-forum.com/t/re-prototype-design-pattern/98110/7 "2006-07-14T03:19:24Z")

</div>

Christian N. wrote:

> Why not even this:
> 
> ```
> connect { 'do something' }
> 
> inspect { [host, port].join ':' }
> 
> ```

Sweet and fine! My, oh my, it’s looking like a paradigm.

T.
