Ruby 2.4

Class

class Class

Parent:Module

扩展任何类以包含json_creatable?方法。

Ruby中的类是第一类对象 - 每个对象都是类的实例Class

通常,您可以使用以下命令创建一个新类:

class Name # some code describing the class behavior end

当创建一个新类时,一个Class类型的对象被初始化并且被分配给一个全局常量(Name在这种情况下)。

Name.new被调用来创建一个新的对象时,默认运行new方法Class。这可以通过重写被证明newClass

class Class alias old_new new def new(*args) print "Creating a new ", self.name, "\n" old_new(*args) end end class Name end n = Name.new

生产:

Creating a new Name

类,模块和对象是相互关联的。在下面的图中,垂直箭头表示继承,而括号则表示元类。所有的元类都是类“Class”的实例。

+---------+ +-... | | | BasicObject-----|-->(BasicObject)-------|-... ^ | ^ | | | | | Object---------|----->(Object)---------|-... ^ | ^ | | | | | +-------+ | +--------+ | | | | | | | | Module-|---------|--->(Module)-|-... | ^ | | ^ | | | | | | | | Class-|---------|---->(Class)-|-... | ^ | | ^ | | +---+ | +----+ | | obj--->OtherClass---------->(OtherClass)-----------...

公共类方法

new(super_class=Object) → a_class 显示源文件

new(super_class=Object) { |mod| ... } → a_class

用给定的超类创建一个新的匿名(未命名)类(或者Object如果没有给出参数)。您可以通过将类对象分配给常量来为类指定一个名称。

如果给出了一个块,它将传递类对象,并且该块将在此类的上下文中进行评估class_eval

fred = Class.new do def meth1 "hello" end def meth2 "bye" end end a = fred.new #=> #<#<Class:0x100381890>:0x100376b98> a.meth1 #=> "hello" a.meth2 #=> "bye"

如果你想像普通类一样对待类,可以将类指定为一个常量(名称以大写字母开头)。

static VALUE rb_class_initialize(int argc, VALUE *argv, VALUE klass) { VALUE super; if (RCLASS_SUPER(klass) != 0 || klass == rb_cBasicObject) { rb_raise(rb_eTypeError, "already initialized class" } if (argc == 0) { super = rb_cObject; } else { rb_scan_args(argc, argv, "01", &super rb_check_inheritable(super if (super != rb_cBasicObject && !RCLASS_SUPER(super)) { rb_raise(rb_eTypeError, "can't inherit uninitialized class" } } RCLASS_SET_SUPER(klass, super rb_make_metaclass(klass, RBASIC(super)->klass rb_class_inherited(super, klass rb_mod_initialize(klass return klass; }

公共实例方法

allocate() → obj 显示源文件

的新对象分配空间,并且不调用新实例的初始化。返回的对象必须是的实例。

klass = Class.new do def initialize(*args) @initialized = true end def initialized? @initialized || false end end klass.allocate.initialized? #=> false

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 } RUBY_DTRACE_CREATE_HOOK(OBJECT, rb_class2name(klass) obj = (*allocator)(klass if (rb_obj_class(obj) != rb_class_real(klass)) { rb_raise(rb_eTypeError, "wrong instance allocation" } return obj; }

json_creatable?() 显示源文件

如果此类可用于从序列化的JSON字符串创建实例,则返回true。该类必须实现一个类方法json_create,它需要一个散列作为第一个参数。散列应包含所需的数据。

# File ext/json/lib/json/common.rb, line 453 def json_creatable? respond_to?(:json_create) end

new(args, ...) → obj 显示源文件

调用allocate创建的新对象,然后调用该对象的initialize方法,传递它的参数。这是最终在每次使用.new构造对象时调用的方法。

VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass) { VALUE obj; obj = rb_obj_alloc(klass rb_obj_call_init(obj, argc, argv return obj; }

superclass → a_super_class or nil 显示源文件

返回的超,或nil

File.superclass #=> IO IO.superclass #=> Object Object.superclass #=> BasicObject class Foo; end class Bar < Foo; end Bar.superclass #=> Foo

当给定的类没有父类时返回nil:

BasicObject.superclass #=> nil

VALUE rb_class_superclass(VALUE klass) { VALUE super = RCLASS_SUPER(klass if (!super) { if (klass == rb_cBasicObject) return Qnil; rb_raise(rb_eTypeError, "uninitialized class" } while (RB_TYPE_P(super, T_ICLASS)) { super = RCLASS_SUPER(super } if (!super) { return Qnil; } return super; }

私有实例方法

inherited(subclass) 显示源文件

每当创建当前类的子类时调用回调。

Example:

class Foo def self.inherited(subclass) puts "New subclass: #{subclass}" end end class Bar < Foo end class Baz < Bar end

生产:

New subclass: Bar New subclass: Baz

static VALUE rb_obj_dummy(void) { return Qnil; }