From: Christian Boos Date: 2002-04-19T06:03:19+09:00 Subject: RE: Confused by example in Pickaxe book Hello, You should have a look to the signature of those methods. It's pretty simple: > -----Original Message----- > From: Daniel Berger [mailto:djberg96@hotmail.com] > Sent: Thursday, April 18, 2002 5:47 PM > To: ruby-talk ML > Subject: Confused by example in Pickaxe book > > > Hi all, > > On p. 181 of the Pickaxe, the example defines the "initialize" method > like this: > > rb_define_method(cCDPlayer,"initialize",cd_init,1); > ... CDPlayer#initialize will take one fixed argument. > Yet, in the "new" method it's called like this: > > rb_obj_call_init(tdata,1,argv); > This function uses a argc-argv style list of VALUEs. '1' is the count of elements in the C array of VALUEs. Internally, 'rb_obj_call_init' will use 'rb_funcall2' to call the "initialize" method on the 'tdata' object. > First, isn't that 2 arguments instead of 1 (remembering to exclude > 'self', i.e. tdata, as an argument). What is the '1' for? > > Second, what is the purpose of this? > > VALUE argv[1]; > argv[0] = unit; > > What is happening here? > ... when using 'rb_obj_call_init', one has to pack the arguments in a C array of VALUEs, even if there's only one argument. I guess that in that specific case, one could write the call in a shorter way: rb_obj_call_init(tdata,1,&unit); ... but this would be even more confusing :) > Much confused. > Hope it helps :) > Regards, > > Dan > >