Re: prototype design pattern

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.

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.

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

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

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

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

[email protected] 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.

On Thu, 13 Jul 2006 [email protected] 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

[email protected] 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 ':' }

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

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs