[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Self and Ruby Comparisons

Mystifier

1/23/2005 2:51:00 PM

Hi

I could not locate any Self and Ruby comparison. Any links?


Thanks,

Mystifier
37 Answers

tsawyer

1/23/2005 4:34:00 PM

0

I don't know if there is much comparision out there. You can have a
look at the suby-muse and suby-ruby archives. You may find a little bit
of info on the subject there --on occassion we have discussed of
prototype-based Ruby.

T.

Martin DeMello

1/23/2005 8:49:00 PM

0

Trans <tsawyer@gmail.com> wrote:
> I don't know if there is much comparision out there. You can have a
> look at the suby-muse and suby-ruby archives. You may find a little bit
> of info on the subject there --on occassion we have discussed of
> prototype-based Ruby.

for one, self doesn't have a ruby keyword...

right. i'll get my coat.

martin

tsawyer

1/24/2005 3:58:00 AM

0

> for one, self doesn't have a ruby keyword...
>
> right. i'll get my coat.

:-)

Trans

1/24/2005 11:38:00 AM

0

> Btw, I hope you know that Ruby has almost full support
> for prototype based programming.

1) I would like to see a simple demonstration of this, I think while
basically possible, things like traits aren't so easily doable.

2) And even so, the "class-way" will be too tempting for lack of syntax
sugars, I think.

3) Also programming Ruby in this way would likely incur more overahead,
rather than less like in io (I imagine).

Thanks for the info.
T.

Csaba Henk

1/24/2005 4:40:00 PM

0

On 2005-01-24, Trans <transfire@gmail.com> wrote:
>> Btw, I hope you know that Ruby has almost full support
>> for prototype based programming.
>
> 1) I would like to see a simple demonstration of this, I think while
> basically possible, things like traits aren't so easily doable.

Uh, to admit, my above claim is somewhat intuitive.

I have a general understanding of what a prototype based object model
is, and I made a mental experiment what components are needed to support
it, and how these can be implemented in ruby, and I found that they can
be implemented quite easily and naturally.

I don't know what such "subtleties" like traits are. If you gave me a
concise explanation, I'd appreciate that. And then we could think about
my implementation, can/should it be extended to have that feature?

So, my idea is as follows:

A prototype is a module which extends itself.

Cloning is supported out of the box. The good question is inheritance
and that how you realize different method (slot) lookup schemes.

As my prototype is a module, a modification in an instance method is
inherited. You can add/modify methods of a prototype such that its
children are not affected by means of module/singleton methods.

There is also another way of cloning:

module Foo; extend self; end
# Bar will be children of Foo
module Bar; extend self; extend Foo; end

In fact, this is the proper way of cloning. If you do so, sending a
message to Bar which is defined in Foo will be properly dispatched at
Foo. You also get reflection for free: existing methods like
Module#ancerstors will give the correct info here as well.

By mixing in prototypes you gain a nice mechanism to have your prototype
based world interact with traditional ruby code (if it weren't an
important criterion, you could put together custom object models in any
language supporting closures). If you want more, by method_missing you
can make proxy modules to existing objects which give you all the freedom
that you enjoy with prototypes.

You can get your (ie. not basic or third party) classes involved to the
game (of communicating with prototypes, ie. making them possible to be
mixed in; I don't mean here converting them to a prototype based model)
by changing the "class Foo" type opening stanza to "module Foo", and
defining a "new" module method like

def new(*a,&b)
o=Object.new
o.extend self
o.initialize(*a,&b)
o
end

(some subtleties like the "class" instance method need to be worked out
here). Or, you can just use project evil's class-to-module transformer
:)

It's also nice that the initial copy of your prototype can be created in
the usual way, by assigning it to a constant with the

module Foo
...
end

syntax, but copies of it don't need to occupy costants, there you can
use Module.new.

Maybe it would be worth to consider creating a Prototype < Module class
which sugares all these actions (the initialize method of Prototype
would care about the self-extension, etc.)

So, I'm just curious what you think of the power of this approach; is
anything essential missing?

> 2) And even so, the "class-way" will be too tempting for lack of syntax
> sugars, I think.

By my opinion the watershed is not having a class keyword or type or
metatype not, but that in what extent are the important OO mechanisms
(inheritance and co.) are available for "ordinary" objects. By having
singleton methods, the "clone" operation, fully objectified
classes/modules and the possibility of creating proxy modules, the
situation is quite good in ruby.

I don't think of the usual class-based structure as a temptation to
avoid. I do think of prototype-like techniques as a powerful tool which
can be utilized at the right time.

Eg., I tell you about the circumstances amongst which I meditated over
these things.

I was writing a small utility for displaying tree-like structures (in
ascii art in my case, but that could be easily replaced by other
rendering engines, so it's not important). For first, I wrote the code
in mind with that I got serial data to be properly allocated in a fresh
custom structure (eg., reading a directory tree from disk or from the
content listing of a tarball), so I created the necessary classes which
provided this structure.

But then I realized that if I want good, flexible code, I can't exclude
the possibility of displaying something which is not serial, and already
has an implicit tree-like structure (like (nested) Arrays). Converting
such things ("clients") instance-by-instance to my custom structure
(which can be passed to the renderer) is a superfluous overhead. So
rather than doing this, I just wanted to make the implicit structure
explicit by adding the methods to the clients needed by the renderer. I
then converted a bunch of classes to modules, but it was not that easy,
because both class and istance methods were necessary to be added to the
clients to make the renderer happy. The solultion was both extending and
including a module in the client class.

That is, upon necessity, you have alternatives to the stock
"class A < B; include C; ... end; a=A.new" OO. And it's good enough for
me.

I've also seen techniques like self-extending modules, and instead of
having an Foo::Bar type module or class for some task in the context of
Foo, having just a simple Bar object like

module Foo
Bar = Object.new
class << Bar
....
end

in other people's code so I think these ideas are existing ruby
patterns, not just my deviance. Kernel itself is a self-extending
module.

> 3) Also programming Ruby in this way would likely incur more overahead,
> rather than less like in io (I imagine).

I don't think that any of the above would mean an overhead for the
interpreter. As for the programmer, it depends on her needs. If she
wants to use these techniques in a clear, explicit way, she will be able
to add the necessary sugar. Ruby does a good job in this respect.

Csaba

Csaba Henk

1/24/2005 5:41:00 PM

0

On 2005-01-24, Csaba Henk <csaba@phony_for_avoiding_spam.org> wrote:
> In fact, this is the proper way of cloning. If you do so, sending a
> message to Bar which is defined in Foo will be properly dispatched at
> Foo. You also get reflection for free: existing methods like
> Module#ancerstors will give the correct info here as well.

Ah, and one more neat trick: as prototype based objects are instances of
the same class Module (or a customized Prototype class), you can get
just one particular method of one p.b.o as UnboundMethod, and bind it to
another p.b.o. ... You also use here that p.b.o-s extend themselves.

Csaba

Mathieu Bouchard

1/25/2005 9:09:00 AM

0

Robert Klemme

1/25/2005 10:29:00 AM

0


"Mathieu Bouchard" <matju@sympatico.ca> schrieb im Newsbeitrag
news:Pine.LNX.4.21.0501250404130.16754-100000@mondrian.artengine.ca...
>
> On Mon, 24 Jan 2005, Csaba Henk wrote:
>
> > On 2005-01-23, Mystifier <mystifier@users.berlios.de> wrote:
> > Btw, I hope you know that Ruby has almost full support for prototype
> > based programming.
>
> Actually, Perl has it better, because you can change the class of an
> object, and you can change the list of superclasses of any class.
>
> However, this is only a slight advantage, as some other people have
> demonstrated you can do the same for Ruby using not that many lines of C
> code.

And if a copy is sufficient you can do this even in pure Ruby:

class Object
def cast(target_class)
copy = target_class.allocate

instance_variables.each do |var|
copy.instance_variable_set(var, instance_variable_get(var))
end

copy
end
end

>> class Foo
>> attr_accessor :name, :val
>> end
=> nil
>> class Bar
>> attr_accessor :name, :val
>> end
=> nil
>> f=Foo.new
=> #<Foo:0x100c4220>
>> f.name="nnn"
=> "nnn"
>> f.val="vvv"
=> "vvv"
>> b=f.cast Bar
=> #<Bar:0x101a7238 @val="vvv", @name="nnn">
>> b.class
=> Bar

> So the only difference is that in Ruby you can't do full prototype-based
> programming using just 100%-Ruby.

Do you have a link or explanation that demonstrates the merits of the
capability to change an instance's class at runtime? Currently I don't
have a clue in which situations I would want this. Thx!

Kind regards

robert

ts

1/25/2005 10:31:00 AM

0

>>>>> "M" == Mathieu Bouchard <matju@sympatico.ca> writes:

M> Actually, Perl has it better, because you can change the class of an
M> object, and you can change the list of superclasses of any class.

and you forget to say that in this P language array, hash, ... are not
objects.

Always easy to change the class when you work with only *one* type.

M> However, this is only a slight advantage, as some other people have
M> demonstrated you can do the same for Ruby using not that many lines of C
M> code.

You are right, the demonstration was made that a few lines of C can crash
easily ruby ... (personnaly I just need one line to crash ruby)



Guy Decoux




Nicholas Van Weerdenburg

1/25/2005 4:24:00 PM

0

On Tue, 25 Jan 2005 19:30:54 +0900, Robert Klemme <bob.news@gmx.net> wrote:
>
> "Mathieu Bouchard" <matju@sympatico.ca> schrieb im Newsbeitrag
> news:Pine.LNX.4.21.0501250404130.16754-100000@mondrian.artengine.ca...
> >
> > On Mon, 24 Jan 2005, Csaba Henk wrote:
> >
> > > On 2005-01-23, Mystifier <mystifier@users.berlios.de> wrote:
> > > Btw, I hope you know that Ruby has almost full support for prototype
> > > based programming.
> >
> > Actually, Perl has it better, because you can change the class of an
> > object, and you can change the list of superclasses of any class.
> >
> > However, this is only a slight advantage, as some other people have
> > demonstrated you can do the same for Ruby using not that many lines of C
> > code.
>
> And if a copy is sufficient you can do this even in pure Ruby:
>
> class Object
> def cast(target_class)
> copy = target_class.allocate
>
> instance_variables.each do |var|
> copy.instance_variable_set(var, instance_variable_get(var))
> end
>
> copy
> end
> end
>
> >> class Foo
> >> attr_accessor :name, :val
> >> end
> => nil
> >> class Bar
> >> attr_accessor :name, :val
> >> end
> => nil
> >> f=Foo.new
> => #<Foo:0x100c4220>
> >> f.name="nnn"
> => "nnn"
> >> f.val="vvv"
> => "vvv"
> >> b=f.cast Bar
> => #<Bar:0x101a7238 @val="vvv", @name="nnn">
> >> b.class
> => Bar
>
> > So the only difference is that in Ruby you can't do full prototype-based
> > programming using just 100%-Ruby.
>
> Do you have a link or explanation that demonstrates the merits of the
> capability to change an instance's class at runtime? Currently I don't
> have a clue in which situations I would want this. Thx!
>
> Kind regards
>
> robert
>
>

How about migrating legacy objects to new objects at run-time?

Nick
--
Nicholas Van Weerdenburg