[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

java guilt

Giles Bowkett

3/14/2007 8:25:00 PM

I just solved a problem by adding an instance method to String.

It makes for very elegant code elsewhere within the application.

Yet I can't shake the feeling I've done something dirty.

I think there needs to be a group like "Recovering Java Programmers
Anonymous" or somehting.

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://giles.t...

22 Answers

dblack

3/14/2007 8:38:00 PM

0

Hi --

On 3/14/07, Giles Bowkett <gilesb@gmail.com> wrote:
> I just solved a problem by adding an instance method to String.
>
> It makes for very elegant code elsewhere within the application.
>
> Yet I can't shake the feeling I've done something dirty.
>
> I think there needs to be a group like "Recovering Java Programmers
> Anonymous" or somehting.

Feeling squeamish about changing the core language isn't the sole
prerogative of recovering Java programmers :-) There's still no good
answer to the question of how to do it safely and in a way that does
not risk conflicts, though there are various "better practices". My
favorite is "extend", which lets you confine changes to particular
objects. That's not always very efficient, though.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning...)
(See what readers are saying! http://www.r.../r...)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.r...)

Giles Bowkett

3/14/2007 8:56:00 PM

0

On 3/14/07, David A. Black <dblack@wobblini.net> wrote:
> Hi --
>
> On 3/14/07, Giles Bowkett <gilesb@gmail.com> wrote:
> > I just solved a problem by adding an instance method to String.
> >
> > It makes for very elegant code elsewhere within the application.
> >
> > Yet I can't shake the feeling I've done something dirty.
> >
> > I think there needs to be a group like "Recovering Java Programmers
> > Anonymous" or somehting.
>
> Feeling squeamish about changing the core language isn't the sole
> prerogative of recovering Java programmers :-) There's still no good
> answer to the question of how to do it safely and in a way that does
> not risk conflicts, though there are various "better practices". My
> favorite is "extend", which lets you confine changes to particular
> objects. That's not always very efficient, though.

Actually that's my core concern with this kind of thing. Ruby isn't
the fastest language out there. Patching base classes could be pretty
bad for performance in some cases, although in practice it seems to
work out pretty well.

I just tried something:

Fixnum.class_eval do
def +(number)
self - number
end
end

It blew up my irb.

--
Giles Bowkett
http://www.gilesg...
http://gilesbowkett.bl...
http://giles.t...

Raj Sahae

3/14/2007 11:19:00 PM

0

Giles Bowkett wrote:
> I just solved a problem by adding an instance method to String.
>
> It makes for very elegant code elsewhere within the application.
>
> Yet I can't shake the feeling I've done something dirty.
>
> I think there needs to be a group like "Recovering Java Programmers
> Anonymous" or somehting.
>
I've added instance methods to both String and Array. I didn't realize
that I should feel dirty about it.

Raj

Chad Perrin

3/14/2007 11:45:00 PM

0

On Thu, Mar 15, 2007 at 08:19:20AM +0900, Raj Sahae wrote:
> Giles Bowkett wrote:
> >I just solved a problem by adding an instance method to String.
> >
> >It makes for very elegant code elsewhere within the application.
> >
> >Yet I can't shake the feeling I've done something dirty.
> >
> >I think there needs to be a group like "Recovering Java Programmers
> >Anonymous" or somehting.
> >
> I've added instance methods to both String and Array. I didn't realize
> that I should feel dirty about it.

I guess you weren't raised Catholic^H^H^H^H^H^H^H^Hon Java.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"The first rule of magic is simple. Don't waste your time waving your
hands and hopping when a rock or a club will do." - McCloctnick the Lucid

Rob Sanheim

3/15/2007 2:06:00 AM

0

On 3/14/07, Giles Bowkett <gilesb@gmail.com> wrote:
> I just solved a problem by adding an instance method to String.
>
> It makes for very elegant code elsewhere within the application.
>
> Yet I can't shake the feeling I've done something dirty.
>
> I think there needs to be a group like "Recovering Java Programmers
> Anonymous" or somehting.

I worked in Java for 5+ years, and am coming up on my one year
anniversary on working in Ruby professionally. I *still* think adding
methods to core classes should be done very selectively, and only in
cases where that method is truly useful in a wide variety of contexts.
So if adding "foo" to String just makes my code cleaner in one spot
where its called, but I know I probably won't use "foo" anywhere else,
then I'll go with the less elegant solution of a helper method instead
of extending a builtin.

Note that I"m speaking of a larger Rails project with multiple
distributed developers - if its just you and a small side project then
hack up the core classes all you want. =)

- Rob

James Gray

3/15/2007 2:18:00 AM

0

On Mar 14, 2007, at 9:06 PM, Rob Sanheim wrote:

> On 3/14/07, Giles Bowkett <gilesb@gmail.com> wrote:
>> I just solved a problem by adding an instance method to String.
>>
>> It makes for very elegant code elsewhere within the application.
>>
>> Yet I can't shake the feeling I've done something dirty.
>>
>> I think there needs to be a group like "Recovering Java Programmers
>> Anonymous" or somehting.
>
> I worked in Java for 5+ years, and am coming up on my one year
> anniversary on working in Ruby professionally. I *still* think adding
> methods to core classes should be done very selectively, and only in
> cases where that method is truly useful in a wide variety of contexts.

...and...

> Note that I"m speaking of a larger Rails project with multiple
> distributed developers - if its just you and a small side project then
> hack up the core classes all you want. =)

There's quite a bit of irony here my friend. Rails ads a good size
number of methods to the core Ruby classes. ;)

James Edward Gray II

dblack

3/15/2007 2:28:00 AM

0

Hi --

On 3/14/07, Raj Sahae <rajsahae@gmail.com> wrote:
> Giles Bowkett wrote:
> > I just solved a problem by adding an instance method to String.
> >
> > It makes for very elegant code elsewhere within the application.
> >
> > Yet I can't shake the feeling I've done something dirty.
> >
> > I think there needs to be a group like "Recovering Java Programmers
> > Anonymous" or somehting.
> >
> I've added instance methods to both String and Array. I didn't realize
> that I should feel dirty about it.

You shouldn't -- you should just be aware of the potential problems.
Mainly it's the danger of naming conflicts, if we all start
distributing code that adds methods to the core. It's purely a
technical issue, though it's often characterized in quasi-moral terms
("great responsibilty") or stigmatized ("monkey-patching"). My
observation is that these characterizations are usually a prelude to
doing it anyway, even though they don't address the technical problems
directly.

As soon as someone figures out a way to make it work, all will be well
:-) Actually the libraries that allow block-scoped core changes have
never been widely used, as far as I can tell.


David

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning...)
(See what readers are saying! http://www.r.../r...)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.r...)

Rob Sanheim

3/15/2007 3:35:00 AM

0

On 3/14/07, James Edward Gray II <james@grayproductions.net> wrote:
> On Mar 14, 2007, at 9:06 PM, Rob Sanheim wrote:
>
> > On 3/14/07, Giles Bowkett <gilesb@gmail.com> wrote:
> >> I just solved a problem by adding an instance method to String.
> >>
> >> It makes for very elegant code elsewhere within the application.
> >>
> >> Yet I can't shake the feeling I've done something dirty.
> >>
> >> I think there needs to be a group like "Recovering Java Programmers
> >> Anonymous" or somehting.
> >
> > I worked in Java for 5+ years, and am coming up on my one year
> > anniversary on working in Ruby professionally. I *still* think adding
> > methods to core classes should be done very selectively, and only in
> > cases where that method is truly useful in a wide variety of contexts.
>
> ...and...
>
> > Note that I"m speaking of a larger Rails project with multiple
> > distributed developers - if its just you and a small side project then
> > hack up the core classes all you want. =)
>
> There's quite a bit of irony here my friend. Rails ads a good size
> number of methods to the core Ruby classes. ;)
>
> James Edward Gray II

True - and some of the things ActiveSupport adds are things I wouldn't
add myself, had I built Rails :). I think most of the extensions make
sense, given the type of applications Rails are targeting.

- Rob

James Gray

3/15/2007 3:42:00 AM

0

On Mar 14, 2007, at 10:35 PM, Rob Sanheim wrote:

> On 3/14/07, James Edward Gray II <james@grayproductions.net> wrote:
>> On Mar 14, 2007, at 9:06 PM, Rob Sanheim wrote:
>>
>> > On 3/14/07, Giles Bowkett <gilesb@gmail.com> wrote:
>> >> I just solved a problem by adding an instance method to String.
>> >>
>> >> It makes for very elegant code elsewhere within the application.
>> >>
>> >> Yet I can't shake the feeling I've done something dirty.
>> >>
>> >> I think there needs to be a group like "Recovering Java
>> Programmers
>> >> Anonymous" or somehting.
>> >
>> > I worked in Java for 5+ years, and am coming up on my one year
>> > anniversary on working in Ruby professionally. I *still* think
>> adding
>> > methods to core classes should be done very selectively, and
>> only in
>> > cases where that method is truly useful in a wide variety of
>> contexts.
>>
>> ...and...
>>
>> > Note that I"m speaking of a larger Rails project with multiple
>> > distributed developers - if its just you and a small side
>> project then
>> > hack up the core classes all you want. =)
>>
>> There's quite a bit of irony here my friend. Rails ads a good size
>> number of methods to the core Ruby classes. ;)
>>
>> James Edward Gray II
>
> True - and some of the things ActiveSupport adds are things I wouldn't
> add myself, had I built Rails :). I think most of the extensions make
> sense, given the type of applications Rails are targeting.

Exactly. So you are more in favor of the process than you thought. ;)

James Edward Gray II

Jos Backus

3/15/2007 5:52:00 AM

0

Hi David,

On Thu, Mar 15, 2007 at 11:27:43AM +0900, David A. Black wrote:
[snip]
> As soon as someone figures out a way to make it work, all will be well
> :-) Actually the libraries that allow block-scoped core changes have
> never been widely used, as far as I can tell.

Are you referring to libraries like `scope-in-state'? AfaIac, these don't do
what I want them to do, which is the ability to restrict changes made to some
class while executing code in some other class. This is probably expensive as
hell and non-trivial to implement. But it sure would be great to have as it
would make Ruby a "safer" language.

Imagine if you could add String#foo to be visible within class Bar only
without having to subclass String and use it inside of Bar instead of String
(arguably the cleanest way to make this work otherwise).

Cheers,
--
Jos Backus
jos at catnook.com