Forum: Ruby overriding NArray.new

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D2bc07ef3dd6e18824932ce55b1f5571?d=identicon&s=25 fugalh (Guest)
on 2005-11-18 23:34
(Received via mailing list)
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?
Fee23d1fc58edee59e05d7a52dcf172e?d=identicon&s=25 blargity (Guest)
on 2005-11-18 23:40
(Received via mailing list)
On Friday 18 November 2005 16:32, Hans Fugal 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. :-)
D2bc07ef3dd6e18824932ce55b1f5571?d=identicon&s=25 fugalh (Guest)
on 2005-11-19 00:25
(Received via mailing list)
> 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.
D2bc07ef3dd6e18824932ce55b1f5571?d=identicon&s=25 fugalh (Guest)
on 2005-11-19 00:34
(Received via mailing list)
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.
This topic is locked and can not be replied to.