[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Redefining core classes

Jacob Harris

7/8/2006 1:33:00 AM

Hello,

Yeah, it could be dangerous. But it's also incredibly useful at
times. For instance, in Rails it's possible to write declarations like

30.minutes.ago

that allow you to express units of time and space as Ruby
declarations. However, if you look at Numeric in Ruby, there is no
such method 'minutes' or 'ago' defined. Rails adds them, so they can
have them defined for all Numeric objects for convenience. To see why
this is convenient, imagine how that code would be in the traditional
model where you'd extend through inheritance. Rails would have to
define a class RailsFixnum that adds those methods and the user would
have to code this as

x = RailsFixnum.new(30)
x.minutes.ago

Ugly and more error prone. Ruby code is supposed to be consistent and
concise, and the flexibility of modifying base classes helps that
sometimes. It's democratic. You can work with main classes to suit
them better for your needs. I think it's excellent to modify and
extend core classes for your needs, but be sure to test!

Jake

On Jul 6, 2006, at 7:22 PM, Patrick Hurley wrote:

> On 7/6/06, Damaris Fuentes <dfl_maradentro@yahoo.es> wrote:
>> My question is… if I run
>> this file with the new definitions..., does the core String class of
>> Ruby remain changed forever? I dunno…is not this feature a little
>> dangerous?
>
> "Ruby, you'll shoot your eye out"
>
> It is changed until you unchange it, or the interpreter stops -- not
> forever. But yes changing core classes is "dangerous." But so are most
> things worth doing or having. To quote another movie:
>
> "with great power comes great responsibility"
>
> Write unit tests and enjoy.
> pth
>