On Tue, 27 Sep 2005, Bob Hutchison wrote: > Hi, > > I am writing a library for Ruby programmers. What are the issues with adding > methods to the Object class? I'm not talking about the practicalities of it > -- those are as easy as could be and work. What I am concerned with is the > reaction a programmer using that library might have. The benefit is a > simplification of a particularly error-prone, tedious, ugly, and repetitive > test in the library user's code (that might not show up in unit tests). I > don't know what the disadvantages might be, name conflict I suppose. Any > others? Any guidelines as to when extending standard classes is considered > okay, or not? Is there an expectation in the Ruby culture? is this kind of > thing considered rude, or worse? > > Cheers, > Bob > > ---- > Bob Hutchison -- blogs at <http://www.recursive.ca/hutch/> > Recursive Design Inc. -- <http://www.recursive.ca/> > Raconteur -- <http://www.raconteur.info/> is there any reason you can't extend the bjectinstead of classes? for example: harp:~ > cat a.rb require 'set' module ToCSV def to_csv to_a.join ',' end end array = Array::new set = Set::new array << 42 << 'forty-two' set << 42 << 'forty-two' array.extend ToCSV set.extend ToCSV p array.to_csv p set.to_csv harp:~ > ruby a.rb "42,forty-two" "forty-two,42" here neither Array nor Set is affected - only those specific objects. arrayfields actually works this way and add but a single method to the builtin Array class which looks something like: def fields= fields extend ArrayFields unless ArrayFields === self @fields = fields end this keeps namespace to a minimum. you can also provide factories that do this: def csv_dumpable object obj = object.dup obj.extend ToCSV obj end puts(csv_dumpable([0,1,2,3,42])) and various other tricks. if you must pollute built-ins at least pick good names and add the smallest number of methods possible. yaml does this and it's fine. hth. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | Your life dwells amoung the causes of death | Like a lamp standing in a strong breeze. --Nagarjuna ===============================================================================