From: Lyle Johnson Date: 2003-01-28T23:41:14+09:00 Subject: Re: initialize() and C extensions Tim Bates wrote: > I'm having a problem with extending TCPSocket, and after some discussion on > #ruby-lang it seems that the problem is due to 1.6.8's incorrect handling of > C extensions and initialize(). > > The problem is demonstrated in this piece of code on 1.6.8, but not on 1.7 > > tim@zaphod:~$ cat b.rb > require 'socket' > class MySocket < TCPSocket > def initialize(address, port, myarg) > end > end > > m = MySocket.new('localhost', 9999, 'foo') > tim@zaphod:~$ ruby b.rb > b.rb:7:in `new': wrong # of arguments(3 for 2) (ArgumentError) > from b.rb:7 > tim@zaphod:~$ Look at the source code for the TCPSocket class from Ruby 1.6.8 (in ext/socket.c). The 'new' singleton method -- which is *different* from the 'initialize' instance method -- is defined to take two arguments. So this ArgumentError message is correct, you're passing three arguments when it only expects two. In Ruby 1.7, the TCPSocket class no longer defines its own 'new' singleton method but instead uses the base class version from IO. That version of 'new' accepts an arbitrary number of arguments, and that's why your code works as expected under Ruby 1.7.