# Class::new :: --\> NUBY

**URL:** <https://www.ruby-forum.com/t/class-new-nuby/132387>\
**Category:** Ruby\
**Created:** [March 19, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/class-new-nuby/132387 "2008-03-19T17:00:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 19, 2008, 5:00pm UTC](https://www.ruby-forum.com/t/class-new-nuby/132387/1 "2008-03-19T17:00:37Z")

</div>

Hi,  
I was trying to redefine new in my class…like this…

class KK  
alias oldnew new  
def new  
yield if block\_given?  
oldnew  
end  
end  
NameError: undefined method `new' for class`KK’

why is saying no ‘new’ method…I don’t understand.  
because I can say KK.new after I define the class which means the method  
is there in the class hierarchy and I should be able to override its  
implementation…

THnks  
Chinna

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 19, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/class-new-nuby/132387/2 "2008-03-19T17:38:03Z")

</div>

Chinna K. wrote:

> class KK  
> alias oldnew new

> NameError: undefined method `new' for class`KK’

You are trying to alias the _instance_ method #new which doesn’t exist.  
#new is a _class_ method so you need to alias it in the metaclass.  
Something like:

class KK  
end

class \<\< KK  
alias\_method :oldnew, :new  
def new  
yield if block\_given?  
oldnew  
end  
end

(You need the class to exist first before you access the metaclass.)

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 19, 2008, 6:12pm UTC](https://www.ruby-forum.com/t/class-new-nuby/132387/3 "2008-03-19T18:12:33Z")

</div>

On Wed, 19 Mar 2008 11:00:28 -0500, Chinna K. wrote:

> end  
> NameError: undefined method `new' for class`KK’
> 
> why is saying no ‘new’ method…I don’t understand. because I can say  
> KK.new after I define the class which means the method is there in the  
> class hierarchy and I should be able to override its implementation…
> 
> THnks  
> Chinna

I think you want to define the constructor for class KK as:

class KK  
def initialize  
yield if block\_given  
super  
end  
end

Does this do what you’re trying to accomplish?

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 19, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/class-new-nuby/132387/4 "2008-03-19T20:12:36Z")

</div>

On Wed, 19 Mar 2008 12:23:58 -0500, Mateusz T. wrote:

> [Note: parts of this message were removed to make it a legal post.]
> 
> Try this code:  
> class KK  
> def new  
> yield if block\_given?  
> super  
> end  
> end

This won’t work, because it operates on an instance method named new.

–Ken

---

<div class="post-metadata">

**Author:** ![chinnakaruppan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@chinnakaruppan](https://www.ruby-forum.com/u/chinnakaruppan)\
**Post date:** [March 19, 2008, 6:25pm UTC](https://www.ruby-forum.com/t/class-new-nuby/132387/5 "2008-03-19T18:25:00Z")

</div>

Try this code:  
class KK  
def new  
yield if block\_given?  
super  
end  
end

2008/3/19, Chinna K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
