# Overriding NArray.new

**URL:** <https://www.ruby-forum.com/t/overriding-narray-new/48330>\
**Category:** Ruby\
**Created:** [November 18, 2005, 11:34pm UTC](https://www.ruby-forum.com/t/overriding-narray-new/48330 "2005-11-18T23:34:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fugalh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/48db29/32.png) [@fugalh](https://www.ruby-forum.com/u/fugalh)\
**Post date:** [November 18, 2005, 11:34pm UTC](https://www.ruby-forum.com/t/overriding-narray-new/48330/1 "2005-11-18T23:34:10Z")

</div>

class Sound \< NArray  
def initialize  
puts ‘Chunky. Bacon.’  
end  
end

Sound.new(4,10) #=\> Sound.sfloat(10): [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,  
0.0, 0.0, 0.0, 0.0 ]

I’m not sure why this is, but I bet it has something to do with the way  
NArray is implemented in C. That’s keen, but I’d really like to  
override new (I’d do something more useful than above, I promise).  
Overriding other methods of NArray is no problem. Is there any hope?

---

<div class="post-metadata">

**Author:** ![fugalh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/48db29/32.png) [@fugalh](https://www.ruby-forum.com/u/fugalh)\
**Post date:** [November 18, 2005, 11:40pm UTC](https://www.ruby-forum.com/t/overriding-narray-new/48330/2 "2005-11-18T23:40:13Z")

</div>

On Friday 18 November 2005 16:32, Hans F. wrote:

> I’m not sure why this is, but I bet it has something to do with the way  
> NArray is implemented in C. That’s keen, but I’d really like to  
> override new (I’d do something more useful than above, I promise).  
> Overriding other methods of NArray is no problem. Is there any hope?

Never used NArray before, but try calling super() with the args you’d  
normally  
give an NArray if it wasn’t a Sound so that NArray’s initialize method  
gets  
called. 🙂

---

<div class="post-metadata">

**Author:** ![fugalh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/48db29/32.png) [@fugalh](https://www.ruby-forum.com/u/fugalh)\
**Post date:** [November 19, 2005, 12:34am UTC](https://www.ruby-forum.com/t/overriding-narray-new/48330/3 "2005-11-19T00:34:39Z")

</div>

Ah, I figured it out:

class Sound \< NArray  
def self.new(typecode, frames, channels)  
super  
end  
end

That’s an oversimplified version of the end product.

---

<div class="post-metadata">

**Author:** ![fugalh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/48db29/32.png) [@fugalh](https://www.ruby-forum.com/u/fugalh)\
**Post date:** [November 19, 2005, 12:25am UTC](https://www.ruby-forum.com/t/overriding-narray-new/48330/4 "2005-11-19T00:25:36Z")

</div>

> Never used NArray before, but try calling super() with the args you’d  
> normally give an NArray if it wasn’t a Sound so that NArray’s  
> initialize method gets called. 🙂

Yup, I do want to call super. My example was contrived such as to make  
it painfully obvious that even if you don’t call super, NArray’s  
initialize gets magically called instead of the subclass’s.
