[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [ANN] prototype-2.0.0

Victor 'Zverok' Shepelev

6/5/2007 6:56:00 PM

From: ara.t.howard [mailto:ara.t.howard@gmail.com]
Sent: Tuesday, June 05, 2007 6:33 AM


>NAME
> prototype.rb
>

Several small questions about the library:

* Does cloned object has some connections with it's prototype (as per Io:
"prototype is something knowing how to process messages I don't know")

* If so, can object's prototype be changed dynamically? (I mean, b.proto =
c)

* And what about Io-like "prototype is context for code blocks?":

block = lambda{puts a + b}

block.call #=> undefined local variable or method `a'

p = Prototype.new{
@a = 40
@b = 2
}

block.proto = p

bloc.call #=> 42

BTW, Markaby uses complicated hack to "setup context variables, while
building HTML" - which is very close to the above example.

* Does anybody uses it in real projects? I mean, does prototype-based
approach in not prototype-based language have proved to be useful? The
question is not about "throw the library away", but about "who can say, what
features in library are useful", "who can recommend library for some tasks?"

V.


3 Answers

ara.t.howard

6/5/2007 8:03:00 PM

0


On Jun 5, 2007, at 12:55 PM, Victor Zverok Shepelev wrote:

> From: ara.t.howard [mailto:ara.t.howard@gmail.com]
> Sent: Tuesday, June 05, 2007 6:33 AM
>
>
>> NAME
>> prototype.rb
>>
>
> Several small questions about the library:
>
> * Does cloned object has some connections with it's prototype (as
> per Io:
> "prototype is something knowing how to process messages I don't know")
>

yes. if one does

clone = Object.prototype{ @a = 42 }.clone

the clone has a #a method from parent and @a instance var if it's own


> * If so, can object's prototype be changed dynamically? (I mean,
> b.proto =
> c)
>

no. but they can be extended

proto = Object.prototype

proto.prototyping do
@a = 42

def a() @a end
end

> * And what about Io-like "prototype is context for code blocks?":
>
> block = lambda{puts a + b}
>
> block.call #=> undefined local variable or method `a'
>
> p = Prototype.new{
> @a = 40
> @b = 2
> }
>
> block.proto = p
>
> bloc.call #=> 42
>

sortof.

cfp:~ > cat a.rb
require 'prototype'

block = lambda{ puts a + b }

proto = Object.prototype

proto.prototyping do
a 40
b 2
c &block
end

proto.c


cfp:~ > ruby a.rb
42


i think your exact example would be quite easy to impliment

> BTW, Markaby uses complicated hack to "setup context variables, while
> building HTML" - which is very close to the above example.
>

check out prototype.rb impl - it's very clean imho. nothing is off
limits - you can have attribute 'id', 'name', 'object_id', etc and
everything works.


> * Does anybody uses it in real projects? I mean, does prototype-based
> approach in not prototype-based language have proved to be useful? The
> question is not about "throw the library away", but about "who can
> say, what
> features in library are useful", "who can recommend library for
> some tasks?"

example

Config = Object.prototype{
YAML.load(IO.read('config')).each do |key, value|
attribute key => value
end
}

p Config.host
p Config.port

i'm using it in several project where one might instead use a
singleton. the advantage prototype gives you is basically that you
can have a hierarchy of singletons, each inheriting state and
behavior from it's parent. i'm currently building a model->view (no
controller) system for rails called 'magnetic' that hinges upon the
ability to specify a hierarchy of properties and behavior over those
properties and also that requires that each set of properties be a
singleton (for performance reasons) and it's filling that roll
nicely. look for a release in the next few weeks.

kind regards.

-a
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




James Gray

6/5/2007 8:20:00 PM

0

On Jun 5, 2007, at 3:03 PM, ara.t.howard wrote:

>
> On Jun 5, 2007, at 12:55 PM, Victor Zverok Shepelev wrote:
>
>> * Does anybody uses it in real projects? I mean, does prototype-based
>> approach in not prototype-based language have proved to be useful?
>> The
>> question is not about "throw the library away", but about "who can
>> say, what
>> features in library are useful", "who can recommend library for
>> some tasks?"
>
> example
>
> Config = Object.prototype{
> YAML.load(IO.read('config')).each do |key, value|
> attribute key => value
> end
> }
>
> p Config.host
> p Config.port

Which is significantly better than:

require "ostruct"
Config = OpenStruct.new(
File.open("config") { |file| YAML.load(file) }
)

?

I guess the difference comes in how they are used, right? Can you
show an example of that?

I realize I'm just not prototype enlightened yet. No offense intended.

James Edward Gray II

ara.t.howard

6/5/2007 9:45:00 PM

0


On Jun 5, 2007, at 2:19 PM, James Edward Gray II wrote:

> On Jun 5, 2007, at 3:03 PM, ara.t.howard wrote:
>
>>
>> On Jun 5, 2007, at 12:55 PM, Victor Zverok Shepelev wrote:
>>
>>> * Does anybody uses it in real projects? I mean, does prototype-
>>> based
>>> approach in not prototype-based language have proved to be
>>> useful? The
>>> question is not about "throw the library away", but about "who
>>> can say, what
>>> features in library are useful", "who can recommend library for
>>> some tasks?"
>>
>> example
>>
>> Config = Object.prototype{
>> YAML.load(IO.read('config')).each do |key, value|
>> attribute key => value
>> end
>> }
>>
>> p Config.host
>> p Config.port
>
> Which is significantly better than:
>
> require "ostruct"
> Config = OpenStruct.new(
> File.open("config") { |file| YAML.load(file) }
> )
>
> ?

yes, i think so:

cfp:~ > cat a.rb
require 'rubygems'
require 'prototype'
require 'ostruct'
require 'yaml'

hash = {
:id => 42,
:send => 'ara.t.howard@gmail.com',
:class => 'first',
}

running 'prototype' do
config = prototype.configured hash
p config.id
p config.send
p config.class
end

running 'ostruct' do
config = OpenStruct.new hash
p config.id
p config.send
p config.class
end


BEGIN {
def running name
puts "<======== #{ name } ========"
begin
yield
rescue => e
m, c, b = e.message, e.class, e.backtrace.join("\n")
puts "#{ m }(#{ c })\n#{ b }"
ensure
puts
end
end
}


cfp:~ > ruby a.rb
<======== prototype ========
42
"ara.t.howard@gmail.com"
"first"

<======== ostruct ========
a.rb:21: warning: Object#id will be deprecated; use Object#object_id
125270
no method name given(ArgumentError)
a.rb:22:in `send'
a.rb:22
a.rb:31:in `running'
a.rb:19


realize too that using a prototype for a Config class is but a tiny
tiny sample of where it might benefit someone. anywhere you have a
singleton or a copy constructor it's worth considering.

i'll have some magnetic code posted soon - you can look at that for
an example of grittier prototype use.

kind regards.

-a
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama