[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Adding, removing and redefining features at runtime

Thomas Sondergaard

9/26/2003 12:51:00 AM

I am working on an article on the subject of implementing dynamically typed
languages on the .net CLR. In .net Types are immutable, ie. once defined
they cannot be changed. Writing about this I needed to cover the problem of
namespace collisions. Most modern languages including ruby has introduced
namespaces to alleviate the namespace collision problem, but the convention
of adding your own methods to existing classes reintroduces the problem.

Is ruby merely being pragmatic by allowing you to do this, because it seldom
causes any real problems? Or is it inherently wrong and should not be
permitted by the language/object model?

How do you feel about this feature. Would it be a big loss if ruby didn't
support it? My own feelings are ambivalent. I have found myself adding
features to Time, Enumerable, Module and when I do it I always appreciate
how nice it is that the feature is where it belongs, but I always have this
nagging fear of collisions that I push to the back of my mind.

Curious Tom


7 Answers

Gavin Sinclair

9/26/2003 1:47:00 AM

0

> I am working on an article on the subject of implementing dynamically
> typed languages on the .net CLR. In .net Types are immutable, ie. once
> defined they cannot be changed. Writing about this I needed to cover the
> problem of namespace collisions. Most modern languages including ruby
> has introduced namespaces to alleviate the namespace collision problem,
> but the convention of adding your own methods to existing classes
> reintroduces the problem.
>
> Is ruby merely being pragmatic by allowing you to do this, because it
> seldom causes any real problems? Or is it inherently wrong and should
> not be permitted by the language/object model?

It''s controversial, that''s for sure, and well worth writing an article
about, as the risks are obvious but not all of the benefits are.

It is wrong to argue that it''s inherently wrong. It''s also wrong to argue
that it''s inherently right. You could say that Ruby is being pragmatic
because it seldom causes problems, but you need to emphasise the
responsibility of the programmer to make sure their code behaves nicely.

> How do you feel about this feature. Would it be a big loss if ruby
> didn''t support it? My own feelings are ambivalent. I have found myself
> adding features to Time, Enumerable, Module and when I do it I always
> appreciate how nice it is that the feature is where it belongs, but I
> always have this nagging fear of collisions that I push to the back of
> my mind.

The nagging feeling is justifiable. Nobody wants namespace collisions,
and they should be avoided by good management, not good luck. Sometimes
it''s easy to justify the addition of some or other method, sometimes it''s
not. So you can put your extra methods in a module and extend individual
objects with that module, instead of including it in classes. That
narrows your impact and allows you to keep track of what''s going on, and
should go some way to reducing that nagging feeling.

The main thing is that Ruby represents a significant step forward in
popular computing [1], and you need to embrace some danger to make that
progress. Fortunately, in one''s own programs, just embracing a tiny bit
of "danger" can make a large practical difference, e.g. modifiying the way
an object represents itself just for the purpose of a unit test.

Two benefits of Ruby''s approach that are probably underestimated by people
who, for whatever reason, like to cling to static typing:

- the ability of library writers to create powerful frameworks,
thus containing the "risk" in a way that can be studied, discussed
and tested

- the ability of everyday programmers to make small but meaningful
improvements or bugfixes to the language or libraries, thus
achieving independence and inspiring change based on working
examples

At the end of the day, it''s better to solve problems pragmatically than
dogmatically :) I look forward to reading your article.

> Curious Tom

Cheers,
Gavin

[1] The groundwork was done by LISP, Smalltalk, Perl et al, but that does
not diminish Ruby at all.

PS. I am launching a project that consolidates popular extensions to the
standard classes. Look at project "extensions" on RubyForge if you''re
interested. The CVS will hopefully be there within 7 days. A release
will be a few more weeks. This is inspired by that nagging feeling.
Instead of implementing common modifications in my code, I can rely on a
reference implementation that is documented and tested.



Sean O'Dell

9/26/2003 2:27:00 AM

0

Thomas Sondergaard wrote:
>
> Is ruby merely being pragmatic by allowing you to do this, because it seldom
> causes any real problems? Or is it inherently wrong and should not be
> permitted by the language/object model?
>
> How do you feel about this feature. Would it be a big loss if ruby didn''t
> support it? My own feelings are ambivalent. I have found myself adding
> features to Time, Enumerable, Module and when I do it I always appreciate
> how nice it is that the feature is where it belongs, but I always have this
> nagging fear of collisions that I push to the back of my mind.

I like the freedom, and I have yet to experience any problem with it.
The benefits far outweigh the concerns, and I think that the concerns --
along with their solutions (ala Java) -- stretch too far (although I
don''t doubt people have their stories here and there). Further, I
simply don''t like a language telling me that I can''t do something
because it may be unsafe.

Sean O''Dell


Robert Klemme

9/26/2003 9:06:00 AM

0


"Thomas Sondergaard" <thomas@FirstNameGoesHereSondergaard.com> schrieb im
Newsbeitrag news:3f738d97$0$48913$edfadb0f@dtext02.news.tele.dk...
> I am working on an article on the subject of implementing dynamically
typed
> languages on the .net CLR. In .net Types are immutable, ie. once defined
> they cannot be changed. Writing about this I needed to cover the problem
of
> namespace collisions. Most modern languages including ruby has
introduced
> namespaces to alleviate the namespace collision problem, but the
convention
> of adding your own methods to existing classes reintroduces the problem.
>
> Is ruby merely being pragmatic by allowing you to do this, because it
seldom
> causes any real problems? Or is it inherently wrong and should not be
> permitted by the language/object model?
>
> How do you feel about this feature. Would it be a big loss if ruby
didn''t
> support it? My own feelings are ambivalent. I have found myself adding
> features to Time, Enumerable, Module and when I do it I always
appreciate
> how nice it is that the feature is where it belongs, but I always have
this
> nagging fear of collisions that I push to the back of my mind.

It would definitely be a loss for me. A typical development cycle
involves using irb to try things out and redefine methods if the old
definition didn''t work out. So it''s crucial for this development style.
It makes things go faster because you can reapply methods on existing
state which you don''t have to recreate during each debugging / development
cycle.

Also, as I leared recently, this seems to be a typical develpment style
with Lisp, too. Lisp goes even further in that it provides for
recompilation of functions to machine code from within a running system!
See here for an example report that emphasises modification of a running
system (Chapter 9)
http://www.ess.stsci.edu/psdb/spike/spike-wg/psflang-...

Kind regards

robert

Marcin 'Qrczak' Kowalczyk

9/26/2003 9:19:00 AM

0

W li?cie z pi?, 26-09-2003, godz. 03:04, Thomas Sondergaard pisze:

> Most modern languages including ruby has introduced
> namespaces to alleviate the namespace collision problem, but the convention
> of adding your own methods to existing classes reintroduces the problem.
>
> Is ruby merely being pragmatic by allowing you to do this, because it seldom
> causes any real problems? Or is it inherently wrong and should not be
> permitted by the language/object model?

For me it''s essential that you can add new generic functions which
dispatch on old types, and that you can add new types on which old
generic functions dispatch. I refuse to like languages which don''t
allow both.

It''s not essential that the language is object oriented though, and
I don''t like being forced to put a name into a shared namespace. The
traditional OO style also allows to dispatch on one parameter only.

So, in Ruby it''s an essential feature with a bad side effect. I don''t
like how it is done (that you "add a method to a class" and risk name
clashes), but it''s great that it can be done at all.

Languages which can do both without the namespace issue: Lisp (CLOS),
Dylan, and to some extent Haskell, Clean and Mercury (where the dispatch
is limited and usually can be resolved statically).

--
__("< Marcin Kowalczyk
\__/ qrczak@knm.org.pl
^^ http://qrnik.knm.org.p...


Joe Cheng

9/26/2003 1:22:00 PM

0

I''m generally a static typing kind of guy, but have been trying to get into
the Ruby mindset as well. If Java or C# types were mutable (if it were even
possible), it would horrify me. I use those languages because I value the
rigidity of the compile-time checks and mutable types would totally
undermine that.

But I use Ruby when I value productivity over compile-time checking, so I
don''t think it would be an appropriate tradeoff to sacrifice the ability to
dynamically add methods. It''s harder to break something without knowing it
in Java than in Ruby, but it''s just so much faster to make something work in
Ruby. I really value having both at my disposal.


Martin DeMello

9/26/2003 2:51:00 PM

0

Gavin Sinclair <gsinclair@soyabean.com.au> wrote:

> PS. I am launching a project that consolidates popular extensions to the
> standard classes. Look at project "extensions" on RubyForge if you''re
> interested. The CVS will hopefully be there within 7 days. A release
> will be a few more weeks. This is inspired by that nagging feeling.
> Instead of implementing common modifications in my code, I can rely on a
> reference implementation that is documented and tested.

Excellent idea. Two questions, though - how do you define popularity,
and how do you avoid bloat?

martin

Eric Hodel

9/27/2003 10:37:00 PM

0

Thomas Sondergaard (thomas@FirstNameGoesHereSondergaard.com) wrote:

> How do you feel about this feature. Would it be a big loss if ruby didn''t
> support it?

This feature makes porting or refactoring code in Ruby a dream.

Take this code, for example:

class Module
private
def move_class_methods (cls, *attrs)
attrs.each {|attr|
module_eval(<<-EOS)
def self.#{attr}(*args)
$stderr.puts "WARNING: #{cls}.#{attr} called from " + caller[0]
#{cls}.#{attr}(*args)
end
EOS
}
end
end

It allows you to easily redefine/move class methods from one class
to another:

class OldLocation
move_class_methods(NewLocation, :foo)

def bar
return self.foo(1, 2)
end
end

class NewLocation
def self.foo(x, y)
return x * y + y ** 2 - 3
end
end

You could write similar code to aid in refactoring instance methods.

--
Eric Hodel - drbrain@segment7.net - http://se...
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04