allocate()
public
Allocates space for a new object of
class’s class and does not call initialize on the new instance. The returned object must be an
instance of class.
klass = Class.new do
def initialize(*args)
@initialized = true
end
def initialized?
@initialized || false
end
end
klass.allocate.initialized?
Show source
VALUE
rb_obj_alloc(VALUE klass)
{
VALUE obj;
rb_alloc_func_t allocator;
if (RCLASS_SUPER(klass) == 0 && klass != rb_cBasicObject) {
rb_raise(rb_eTypeError, "can't instantiate uninitialized class");
}
if (FL_TEST(klass, FL_SINGLETON)) {
rb_raise(rb_eTypeError, "can't create instance of singleton class");
}
allocator = rb_get_alloc_func(klass);
if (!allocator) {
rb_undefined_alloc(klass);
}
if (RUBY_DTRACE_OBJECT_CREATE_ENABLED()) {
const char * file = rb_sourcefile();
RUBY_DTRACE_OBJECT_CREATE(rb_class2name(klass),
file ? file : "",
rb_sourceline());
}
obj = (*allocator)(klass);
if (rb_obj_class(obj) != rb_class_real(klass)) {
rb_raise(rb_eTypeError, "wrong instance allocation");
}
return obj;
}