Prototype design pattern

Hm, it's not at
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.

···

-----Original Message-----
From: ara.t.howard@noaa.gov [mailto:ara.t.howard@noaa.gov]
Sent: Wednesday, July 12, 2006 8:27 AM
To: ruby-talk ML
Subject: prototype design pattern

anyone know of a good impl for ruby?

regards.

-a

Hm, it's not at
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.

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! :wink: 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

···

On Wed, 12 Jul 2006, Berger, Daniel wrote:
--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama

Why no go the extra step?

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

       connect does{ 'do something' }

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

T.

···

ara.t.howard@noaa.gov wrote:

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

       def connect() 'do something' end

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

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

···

On Thu, 13 Jul 2006 transfire@gmail.com wrote:

Why no go the extra step?

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

      connect does{ 'do something' }

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

T.

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

transfire@gmail.com writes:

     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 ':' }
     }

Why not even this:

        connect { 'do something' }

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

···

ara.t.howard@noaa.gov wrote:
  
T.

--
Christian Neukirchen <chneukirchen@gmail.com> http://chneukirchen.org

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

···

On Fri, 14 Jul 2006, Christian Neukirchen wrote:

Why not even this:

       connect { 'do something' }

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

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

Christian Neukirchen 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.