On 7/12/06, ara.t.howard / noaa.gov <ara.t.howard / noaa.gov> wrote:
> On Thu, 13 Jul 2006, Pete wrote:
>
> > Can't imagine how it could be simpler...
> >
> > class SomeUselessClass
> > attr_accessor :a, :b, :c
> > def initialize
> >  @a, @b, @c = 1, 2, 3
> > end
> > end
> >
> > prototype = SomeUselessClass.new
> > prototype.a = 7
> >
> > // create instances from prototype
> > o1 = prototype.clone
> > o2 = prototype.clone
> > o3 = prototype.clone
> >
> > cheers
> > peter
>
> mostly succinctness, but also semantics.  cloning a prototype should not copy
> it's instance variables, but rather stamp out a new instance - sort of like a
> constructor.  here's a snippet from my latest branch:
>
>
>      jib:~/eg/ruby/prototype/prototype-0.1.0 > cat samples/d.rb
>      require 'prototype'
>
>      proto = prototype{ attributes '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)}
>
>
>      jib:~/eg/ruby/prototype/prototype-0.1.0 > ruby -Ilib samples/d.rb
>      1
>      2
>      3
>      1
>      2
>      42
>      1
>      2
>      3
>
>
> note that this
>
>      proto = Prototype{ attributes 'a' => 1, 'b' => 2, 'c' => 3 }
>
> or, even shorter
>
>      proto = Prototype{ a 1; b 2; c 3 }
>
> are the same, but more, than this
>
>    class SomeUselessClass
>      attr_accessor :a, :b, :c
>      def initialize
>        @a, @b, @c = 1, 2, 3
>      end
>    end
>    prototype = SomeUselessClass.new
>
> actually - to have the literal equiv your code would need to be closer to
>
>    class SomeUselessClass
>      ATTRIBUTES = %w( a b c)
>
>      ATTRIBUTES.each do |a|
>        attr_accessor a
>        alias_method "#{ a }?", "#{ a }"
>      end
>
>      def self.attributes() ATTRIBUTES end
>
>      def initialize
>        @a, @b, @c = 1, 2, 3
>      end
>    end
>
>    prototype = SomeUselessClass.new
>
>
> and actually there's even a bit more..
>
> and it's not even golfing!  ;-)
>
> -a
> --
> suffering increases your inner strength.  also, the wishing for suffering
> makes the suffering disappear.
> - h.h. the 14th dali lama
>
>
Gosh! Blink and I miss the fun. Ironically, I've been using traits +
some sugar for something like this but your implementation is more -
well - prototypey! Nice work (again).

Regards,
Sean