> imho, there are two occasions for extending a class:
> 1. When the added methods are useful for that class in general (ie,
> String#rot13, Integer#factors)
> 2. When you're just hackingBut this is all fairly subjective, isn't it?
Extremely
In fact, my first draft of the email said so... but I
revised it, and apparently left that statement out.
What I was getting at here, is that #rot13 could be reasonably applied
to any text; and that is the primary use of strings in ruby: a text
container. Whereas, #remove_html_attributes would only be used on
special types of text: html documents/snippets. In this case, I would
subclass; in the rot13 case, I would extend. But that's just me, you
might feel differently. ![]()
Among some programmers,
String#rot13 might be a really sensible thing to add, but to others
maybe it might seem like cruft. One interesting example from RubyConf
was Rich Kilmer saying that he added methods like #minutes and #days to
Fixnum, so he could say things like15.minutes + 2.hours
It's also very easy to imagine using this for, say, feet and inches:
1.miles + 6.feet + 6.inches
... which is culturally specific, isn't it? I mean, you're not going to
use that if you use the metric system like a civilized people.
These things are great tricks, and very handy. But the ability to do
this belongs in the hands of the last coder to work on it. They
shouldn't be included in libs, etc, unless the express purpose of the
lib is to add them. They shouldn't be a side effect.
But they can be awfully handy in a script, or in small applications.
There are two competing philosophies in OO theory. On one hand, you
have the Platonists who believe that types are eternal forms that can
be found in nature, so if you did your analysis right you'd find those
forms and be able to codify them in your class definitions. On the
other hand, you have the Pragmatists who believe that types are just
shorthand, to be defined and discarded as the situation allows.
I tend to be more Pragmatic when I'm writing code that others won't be
editing/coding around, and Platonic when I'm writing library code.
Ruby is more of a Pragmatist language, which personally I like 'cause
on some deep level it jibes with my own belief in the subjectivity of
experience. So, you know, if you want to extend a class, do it if it
makes your life easier. If it doesn't, then don't do it.
Ruby *lets* you be pragmatic. That doesn't mean Ruby *itself* conforms
to your definition of pragmatic. Check the library code, and you'll
probably find it to be awfully platonic. And that's the way I like it,
because I often have trouble understanding other people's pragmatic
code ![]()
Still, as I said, I'm not trying too say you should program any way
you want. my earlier post was prefaced with "imho" for that reason:
It truly was just my opinion, and you can safely ignore it if you want
![]()
cheers,
Mark
···
On Wed, 6 Oct 2004 08:52:27 +0900, Francis Hwang <sera@fhwang.net> wrote:
On Oct 5, 2004, at 5:22 PM, Mark Hubbart wrote: