# Safe override of Class#new?

**URL:** <https://www.ruby-forum.com/t/safe-override-of-class-new/87763>\
**Category:** Ruby\
**Created:** [March 5, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/safe-override-of-class-new/87763 "2007-03-05T20:52:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 5, 2007, 8:52pm UTC](https://www.ruby-forum.com/t/safe-override-of-class-new/87763/1 "2007-03-05T20:52:44Z")

</div>

class Class  
alias :create :new

```
def new(*a,&b)
  obj = allocate
  obj.extend advice
  obj.send(:initialize,*a,&b)
  return obj
end

```

end

Note #advice is a sprcisl module that provides AOP features.

Like to get the opinions of other Ruby experts on this. What kind of  
potential trouble am I asking for by using this?

Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 5, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/safe-override-of-class-new/87763/2 "2007-03-05T21:39:53Z")

</div>

> Like to get the opinions of other Ruby experts on this. What  
> kind of potential trouble am I asking for by using this?

(Not that I’m a Ruby expert, but anyway…)

You’ll run into a real slowdown of your application. AFAIK,  
creating objects is one of the most expensive operations in any  
OO language. Even when it’s implemented in C, as is done with  
Ruby.

Yeah, sure, go ahead, implement it in Ruby and slow it down by  
a factor of three!.. :}

(Just kidding…)

gegroet,  
Erik V. - [http://www.erikveen.dds.nl/](http://www.erikveen.dds.nl/)

* * *

require “benchmark”

class Class  
alias :create :new

def new\_new(\*a, &b)  
obj = allocate  
obj.send(:initialize, \*a, &b)  
obj  
end  
end

times = 1\_000\_000

Benchmark.bmbm do |bm|  
bm.report(“old\_new”) do  
times.times do  
Object.new  
end  
end

bm.report(“new\_new”) do  
times.times do  
Object.new\_new  
end  
end  
end

* * *

```
           user system total real

```

old\_new 0.680000 0.010000 0.690000 ( 0.687622)  
new\_new 1.880000 0.010000 1.890000 ( 1.889214)

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 6, 2007, 1:39am UTC](https://www.ruby-forum.com/t/safe-override-of-class-new/87763/3 "2007-03-06T01:39:22Z")

</div>

On Mar 5, 3:39 pm, “Erik V.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yeah, sure, go ahead, implement it in Ruby and slow it down by
> 
> times = 1\_000\_000  
> Object.new\_new  
> new\_new 1.880000 0.010000 1.890000 ( 1.889214)

Ouch. Sigh, any way I seem to slice it, AOP in pure Ruby sucks.

T.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 7, 2007, 12:10am UTC](https://www.ruby-forum.com/t/safe-override-of-class-new/87763/4 "2007-03-07T00:10:50Z")

</div>

On Mar 6, 1:38 am, “Trans” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ouch. Sigh, any way I seem to slice it, AOP in pure Rubysucks.

OTOH, do you really want to AOP-ify all Ruby classes? Maybe you can  
apply the advice to a limited subset of classes. The performance hit  
might be acceptable in the end.

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 6, 2007, 8:56am UTC](https://www.ruby-forum.com/t/safe-override-of-class-new/87763/5 "2007-03-06T08:56:57Z")

</div>

Trans schrieb:

> Ouch. Sigh, any way I seem to slice it, AOP in pure Ruby sucks.

Welcome to the club ☹

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Trans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/dfb087/32.png) [@Trans](https://www.ruby-forum.com/u/Trans)\
**Post date:** [March 7, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/safe-override-of-class-new/87763/6 "2007-03-07T21:40:56Z")

</div>

On Mar 6, 6:10 pm, “Paolo Nusco P.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 6, 1:38 am, “Trans” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Ouch. Sigh, any way I seem to slice it, AOP in pure Rubysucks.
> 
> OTOH, do you really want to AOP-ify all Ruby classes? Maybe you can  
> apply the advice to a limited subset of classes. The performance hit  
> might be acceptable in the end.

Thank you. I think that will indeed be enough.

T.
