The new allocation scheme and extensions

In 1.6, when writing an extension which required Data_Wrap_Struct, the
programmer would define the singleton method “new” to handle that. In 1.8, the
scheme seems to be to write an allocate function (to pass to
rb_define_alloc_func) then do the rest of the code in the intialize function.
Indeed, that’s what Class#new does now.

So my question is, when writing extensions, is the “old” way of defining the
singleton method “new” deprecated? Are we to use the new allocate/initialize
way now?

···


Shu-yu Guo

Hi,

···

In message “The new allocation scheme and extensions” on 03/11/02, Shu-yu Guo shu@aria.rufuran.org writes:

So my question is, when writing extensions, is the “old” way of defining the
singleton method “new” deprecated? Are we to use the new allocate/initialize
way now?

If you have chance to update the extension, use new allocation scheme
now. But the old scheme would work during 1.8.x series.

						matz.

You can do this on 1.6:

inline VALUE ruby_16_new(int argc, VALUE * argv, VALUE klass)
{
VALUE obj = rb_funcall(klass, rb_intern(“allocate”), 0);
rb_obj_call_init(obj, argc, argv);
return obj;
}

rb_define_singleton_method(klass, “allocate”, allocate_func, -1);
rb_define_singleton_method(klass, “new”, ruby_16_new, -1);
rb_define_method(klass.v, “initialize”, initialize_func, -1);

This allows you to divide your code into allocate/initialize similar to
how you would on 1.8.

Paul

···

On Sun, Nov 02, 2003 at 06:43:14AM +0900, Shu-yu Guo wrote:

In 1.6, when writing an extension which required Data_Wrap_Struct, the
programmer would define the singleton method “new” to handle that. In 1.8, the
scheme seems to be to write an allocate function (to pass to
rb_define_alloc_func) then do the rest of the code in the intialize function.
Indeed, that’s what Class#new does now.

So my question is, when writing extensions, is the “old” way of defining the
singleton method “new” deprecated? Are we to use the new allocate/initialize
way now?

Hello All,

Has anyone got, or know of a diagram that shows how the ruby classes are
ordered in terms of inheritence?

Thanks,

Thomas Adam

···

=====
Thomas Adam

“The Linux Weekend Mechanic” – http://linuxgazette.net
"TAG Editor" – http://linuxgazette.net


Want to chat instantly with your online friends? Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk

Paul Brannan pbrannan@atdesk.com writes:

In 1.6, when writing an extension which required Data_Wrap_Struct, the
programmer would define the singleton method “new” to handle that. In 1.8, the
scheme seems to be to write an allocate function (to pass to
rb_define_alloc_func) then do the rest of the code in the intialize function.
Indeed, that’s what Class#new does now.

So my question is, when writing extensions, is the “old” way of defining the
singleton method “new” deprecated? Are we to use the new allocate/initialize
way now?

You can do this on 1.6:

inline VALUE ruby_16_new(int argc, VALUE * argv, VALUE klass)
{
VALUE obj = rb_funcall(klass, rb_intern(“allocate”), 0);
rb_obj_call_init(obj, argc, argv);
return obj;
}

rb_define_singleton_method(klass, “allocate”, allocate_func, -1);
rb_define_singleton_method(klass, “new”, ruby_16_new, -1);
rb_define_method(klass.v, “initialize”, initialize_func, -1);

This allows you to divide your code into allocate/initialize similar to
how you would on 1.8.

Or you can do this on 1.6 and 1.8:

static VALUE foo_s_allocate(VALUE klass)
{

}

static VALUE foo_initialize(int argc, VALUE *argv, VALUE self)
{

}

#ifndef HAVE_RB_DEFINE_ALLOC_FUNC
/* ruby 1.6 */
static VALUE foo_s_new(int argc, VALUE * argv, VALUE klass)
{
VALUE obj = foo_s_allocate(klass);
rb_obj_call_init(obj, argc, argv);
return obj;
}
#endif

void Init_foo()
{

#ifdef HAVE_RB_DEFINE_ALLOC_FUNC
/* ruby 1.8 /
rb_define_alloc_func(klass, foo_s_allocate);
#else
/
ruby 1.6 */
rb_define_singleton_method(klass, “new”, foo_s_new, -1);
#endif
rb_define_method(klass, “initialize”, foo_initialize, -1);

}

···

On Sun, Nov 02, 2003 at 06:43:14AM +0900, Shu-yu Guo wrote:


KUBO Takehiro
kubo@jiubao.org

Using ObjectSpace.each_object(Class) and a bit of code (like in the
attached script), you can generate one for yourself. Note that the
below does not mention any included modules (an exercise for the
reader).

Actually, the code I wrote could no doubt be compacted significantly,
any takers?

ruby 1.6.8 (2002-12-24) [i386-freebsd5]
Kernel
Object
Array
Binding
ClassNode
Continuation
Data
Dir
Exception
Interrupt
NoMemoryError
ScriptError
LoadError
NameError
NotImplementedError
SyntaxError
SignalException
StandardError
ArgumentError
IOError
EOFError
IndexError
LocalJumpError
RangeError
FloatDomainError
RegexpError
RuntimeError
SecurityError
SystemCallError
Errno::E2BIG
Errno::EACCES
Errno::EADDRINUSE
Errno::EADDRNOTAVAIL
Errno::EAFNOSUPPORT
Errno::EAGAIN
Errno::EALREADY
Errno::EBADF
Errno::EBUSY
Errno::ECHILD
Errno::ECONNABORTED
Errno::ECONNREFUSED
Errno::ECONNRESET
Errno::EDEADLK
Errno::EDESTADDRREQ
Errno::EDOM
Errno::EDQUOT
Errno::EEXIST
Errno::EFAULT
Errno::EFBIG
Errno::EHOSTDOWN
Errno::EHOSTUNREACH
Errno::EIDRM
Errno::EILSEQ
Errno::EINPROGRESS
Errno::EINTR
Errno::EINVAL
Errno::EIO
Errno::EISCONN
Errno::EISDIR
Errno::ELOOP
Errno::EMFILE
Errno::EMLINK
Errno::EMSGSIZE
Errno::ENAMETOOLONG
Errno::ENETDOWN
Errno::ENETRESET
Errno::ENETUNREACH
Errno::ENFILE
Errno::ENOBUFS
Errno::ENODEV
Errno::ENOENT
Errno::ENOEXEC
Errno::ENOLCK
Errno::ENOMEM
Errno::ENOMSG
Errno::ENOPROTOOPT
Errno::ENOSPC
Errno::ENOSYS
Errno::ENOTBLK
Errno::ENOTCONN
Errno::ENOTDIR
Errno::ENOTEMPTY
Errno::ENOTSOCK
Errno::ENOTTY
Errno::ENXIO
Errno::EOPNOTSUPP
Errno::EOVERFLOW
Errno::EPERM
Errno::EPFNOSUPPORT
Errno::EPIPE
Errno::EPROTONOSUPPORT
Errno::EPROTOTYPE
Errno::ERANGE
Errno::EREMOTE
Errno::EROFS
Errno::ESHUTDOWN
Errno::ESOCKTNOSUPPORT
Errno::ESPIPE
Errno::ESRCH
Errno::ESTALE
Errno::ETIMEDOUT
Errno::ETOOMANYREFS
Errno::ETXTBSY
Errno::EUSERS
Errno::EXDEV
SystemStackError
ThreadError
TypeError
ZeroDivisionError
SystemExit
fatal
FalseClass
File::Stat
Hash
IO
File
MatchData
Method
UnboundMethod
Module
Class
NilClass
Numeric
Float
Integer
Bignum
Fixnum
Proc
Range
Regexp
String
Struct
Struct::Tms
Symbol
Thread
ThreadGroup
Time
TrueClass

···

Thomas Adam (thomas_adam16@yahoo.com) wrote:

Hello All,

Has anyone got, or know of a diagram that shows how the ruby classes are
ordered in terms of inheritence?


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Eric,

Thank you ever so much for that, but do you have the script? Only, It
wasn’t attached to the e-mail :slight_smile:

– Thomas Adam

···

— Eric Hodel drbrain@segment7.net wrote:

Thomas Adam (thomas_adam16@yahoo.com) wrote:

Hello All,

Has anyone got, or know of a diagram that shows how the ruby classes
are
ordered in terms of inheritence?

Using ObjectSpace.each_object(Class) and a bit of code (like in the
attached script), you can generate one for yourself. Note that the
below does not mention any included modules (an exercise for the
reader).

=====
Thomas Adam

“The Linux Weekend Mechanic” – http://linuxgazette.net
“TAG Editor” – http://linuxgazette.net


Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard

Thomas:

You may want to consider this:

http://www.rubygarden.org/ruby?ASCIIClassHierarchyGenerator

It will generate an arbitrary hierachy.

-austin

Dang, oops!

Here it is, no really.

x.rb (1.1 KB)

···

Thomas Adam (thomas_adam16@yahoo.com) wrote:

Thank you ever so much for that, but do you have the script? Only, It
wasn’t attached to the e-mail :slight_smile:


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Since this thread has came up I’ve been doing some digging, thanks to Eric
Hodel and Austin for sparking my interest with their attachments and links.
I’ve been doing some dabbling into the Kernel’s public_method and
private_method to determine what belongs to an object.

I found it useful for myself today doing some Tk stuff so I thought I might
pass it along. Here is a quick implementation of it:

require ‘tk’

ar = TkRoot.public_methods
ar.each{ |x| puts x }

ar = TkRoot.private_methods
ar.each{ |x| puts x }

Zach