From: Trans Date: 2008-02-29T02:27:27+09:00 Subject: Re: Proposed Solutions - Was [ Re: Monkeypatching is Destroying Ruby ] On Feb 28, 9:39 am, hemant wrote: > James, Sometime ago Rick Olson wrote this: > > http://weblog.techno-weenie.net/2007/12/19/adventures-in-rails-debugging > > Does ring a bell, doesn't it? All the libraries Rick mentions in his > blog are well regarded and yet he has a problem. > > From my own experience I have encountered similar problems countless > times and you know these problems are HARD to debug. > > As i wrote earlier, today you have access to source code of all the > libraries you are using because Ruby is source based interpreter and > you can still debug problems because of accidental overrides or > modifying core classes by looking at source code, but what if Rubinius > or IronRuby becomes defacto and people started distributing compiled > bytecodes? > > I propose a small change in the language and perhaps I know it won't > see light of the day, but how about this? > > class Array > def to_csv #> works if in final program no one is actually doing the same > end > end > > However, suppose you use another library and they have done the same, > that code won't run and Ruby will throw up and that sorta makes > sense.However, if you are sure of what you are doing, you can go ahead > and write: > > class Array > def! to_csv #=> similar to override keyword in other languages > end > end > > Ditto, if you want to override methods defined in other classes, you > must use "def!" . > > I know this is somewhat impractical because Ruby has grown into such a > beast, lots of standard library classes and gems will need small > modification ( but we are in midst of such modification anyways > courtesy of 1.9.x ). > > My point is, I love open classes, but it shouldn't hurt to have little > bit of safety precaution built into the language, does it? I think there is deep misunderstanding about all this. The main issue is really that Rails has added so many extensions to Ruby that inexperienced Rails programmers follow suit and extend Ruby willy- nilly. But that's not Ruby's problem. Ruby is an open "agile" language --it lets you override almost any part of the system. Yes, that can be "dangerous", but it can also be extremely powerful. It's up to the programmer to consider their usecase and make the call. No amount of preventative obstruction is going to make anything better, it will only make life more difficult for those who use MP appropriately. I personally think Ruby has too many meta-obstructions as it is. I certainly don't want more. If we can come up with a real solution --eg. a good selector namespaces design, then great. But short of that there is no point. You can already do this: $ cat temp.rb class String def to_s; ""; end end $ ruby -w temp.rb temp.rb:2: warning: method redefined; discarding old to_s That ought to be preventive care enough. T.