Ruby中神秘的数据类是什么?

时间:2022-10-17 22:52:07

Today, I accidentally discovered the mysterious Data class in Ruby, and I can't find any useful information upon what it does, or why it's there. I assume it's part of the language implementation itself.

今天,我偶然发现了Ruby中神秘的Data类,我找不到任何有用的信息,关于它的作用,或者它为什么存在。我认为它是语言实现本身的一部分。

Does anybody know what it does?

有人知道它的作用吗?

mbp-scott:~ scott$ irb
ruby-1.9.3-p0 :001 > Data
=> Data
ruby-1.9.3-p0 :002 > Data.is_a? Module
=> true
ruby-1.9.3-p0 :003 > Data.is_a? Class
=> true
ruby-1.9.3-p0 :004 > Data.ancestors
=> [Data, Object, Kernel, BasicObject]
ruby-1.9.3-p0 :005 > Data.methods
=> [:allocate, :new, :superclass, :freeze, :===, :==, :<=>, :<, :<=, :>,
:>=, :to_s,:included_modules, :include?, :name, :ancestors, :instance_methods,
:public_instance_methods, :protected_instance_methods, :private_instance_methods,
:constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables,
:remove_class_variable, :class_variable_get, :class_variable_set,
:class_variable_defined?, :public_constant, :private_constant, :module_exec,
:class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?,
:private_method_defined?, :protected_method_defined?, :public_class_method
:private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method,
:nil?, :=~, :!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :initialize_dup,
:initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?,
:inspect, :methods, :singleton_methods, :protected_methods, :private_methods,
:public_methods, :instance_variables, :instance_variable_get, :instance_variable_set,
:instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send,
:public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method,
:public_method, :define_singleton_method, :object_id, :to_enum, :enum_for, :equal?,
:!, :!=, :instance_eval, :instance_exec, :__send__, :__id__]
ruby-1.9.3-p0 :006 > Data.instance_variables
=> []
ruby-1.9.3-p0 :007 > self
=> main

3 个解决方案

#1


3  

Might be connected to the C data type RUBY_T_DATA. Which might be a way to store C data via the C API in the Ruby VM.

可能连接到C数据类型RUBY_T​​_DATA。这可能是通过Ruby VM中的C API存储C数据的一种方法。

#2


4  

A few information from C-API documentation:

来自C-API文档的一些信息:

It is defined on object.c. marshal.c uses it as a temporary internal container type here, here and here. stringio.c uses as the superclass of StringIO. error.c uses as the superclass of NameError::message (appears to be inaccessible from ruby side as doesn't start with a capital letter). Is superclass of Iconv in iconv.c.

它在object.c上定义。 marshal.c在此处和此处将其用作临时内部容器类型。 stringio.c用作StringIO的超类。 error.c用作NameError :: message的超类(似乎从ruby端无法访问,因为它不以大写字母开头)。是iconv.c中Iconv的超类。

It is implementation specific and should not be public. Just don't use it.

它是特定于实现的,不应该是公开的。只是不要使用它。

#3


2  

To be fair:

公平起见:

(main)> Class.is_a? Module
=> true

For what I can see you cannot make an instance of it so you wouldn't use it as a clean room (which I thought at first)

我可以看到你无法做出它的实例所以你不会把它用作洁净室(我起初认为)

Here's more from object.c where the docs says it's defined. Seems it shares the same origin as TrueClass and NilClass (to name only those)

这里有更多来自object.c,其中文档说明了它的定义。似乎它与TrueClass和NilClass共享相同的来源(仅列举那些)

rb_define_class("Data", rb_cObject)
rb_undef_alloc_func(rb_cData);

rb_define_class("TrueClass", rb_cObject)
rb_define_class("NilClass", rb_cObject)

Whatever the purpose of that class, it has no methods of it's own and you cannot make an instance of it. Personally I would put my money on useless for now, maybe there are plans for this class in the future and so the const name Data is reserved.

无论该类的目的是什么,它都没有自己的方法,你无法创建它的实例。就个人而言,我现在把钱放在无用的地方,也许将来有这个类的计划,所以const名称数据是保留的。

Edit: There is more... Searching for rb_cData in the codebase turned up examples of it's use.

编辑:还有更多...在代码库中搜索rb_cData会显示它的使用示例。

require 'stringio'
StringIO.ancestors
=> [StringIO, Enumerable, Data, Object, PP::ObjectMixin, Kernel, BasicObject]

It also appears in curses, socket, tk, iconv and in win32ole. Mostly used internally with Data_Wrap_Struct and for defining a base class.

它也出现在curses,socket,tk,iconv和win32ole中。主要在内部使用Data_Wrap_Struct并用于定义基类。

#1


3  

Might be connected to the C data type RUBY_T_DATA. Which might be a way to store C data via the C API in the Ruby VM.

可能连接到C数据类型RUBY_T​​_DATA。这可能是通过Ruby VM中的C API存储C数据的一种方法。

#2


4  

A few information from C-API documentation:

来自C-API文档的一些信息:

It is defined on object.c. marshal.c uses it as a temporary internal container type here, here and here. stringio.c uses as the superclass of StringIO. error.c uses as the superclass of NameError::message (appears to be inaccessible from ruby side as doesn't start with a capital letter). Is superclass of Iconv in iconv.c.

它在object.c上定义。 marshal.c在此处和此处将其用作临时内部容器类型。 stringio.c用作StringIO的超类。 error.c用作NameError :: message的超类(似乎从ruby端无法访问,因为它不以大写字母开头)。是iconv.c中Iconv的超类。

It is implementation specific and should not be public. Just don't use it.

它是特定于实现的,不应该是公开的。只是不要使用它。

#3


2  

To be fair:

公平起见:

(main)> Class.is_a? Module
=> true

For what I can see you cannot make an instance of it so you wouldn't use it as a clean room (which I thought at first)

我可以看到你无法做出它的实例所以你不会把它用作洁净室(我起初认为)

Here's more from object.c where the docs says it's defined. Seems it shares the same origin as TrueClass and NilClass (to name only those)

这里有更多来自object.c,其中文档说明了它的定义。似乎它与TrueClass和NilClass共享相同的来源(仅列举那些)

rb_define_class("Data", rb_cObject)
rb_undef_alloc_func(rb_cData);

rb_define_class("TrueClass", rb_cObject)
rb_define_class("NilClass", rb_cObject)

Whatever the purpose of that class, it has no methods of it's own and you cannot make an instance of it. Personally I would put my money on useless for now, maybe there are plans for this class in the future and so the const name Data is reserved.

无论该类的目的是什么,它都没有自己的方法,你无法创建它的实例。就个人而言,我现在把钱放在无用的地方,也许将来有这个类的计划,所以const名称数据是保留的。

Edit: There is more... Searching for rb_cData in the codebase turned up examples of it's use.

编辑:还有更多...在代码库中搜索rb_cData会显示它的使用示例。

require 'stringio'
StringIO.ancestors
=> [StringIO, Enumerable, Data, Object, PP::ObjectMixin, Kernel, BasicObject]

It also appears in curses, socket, tk, iconv and in win32ole. Mostly used internally with Data_Wrap_Struct and for defining a base class.

它也出现在curses,socket,tk,iconv和win32ole中。主要在内部使用Data_Wrap_Struct并用于定义基类。