0

谁能解释为什么 IronRuby 中的 DateTime 是 Object[] 示例代码

IronRuby 0.9.1.0 on .NET 2.0.50727.4927
Copyright (c) Microsoft Corporation. All rights reserved.

>>> require 'System'
=> true
>>> t = System::DateTime.Now
=> Thu Dec 03 15:32:42 +05:00 2009
>>> t.is_a?(Time)
=> true
>>> t.is_a?(System::DateTime)
=> true
>>> t.is_a?(System::Object)
=> true
>>> t.is_a?(System::Object[])
=> true
>>> t.is_a?(System::Decimal)
=> false
4

2 回答 2

1

因为 System::Object[] 并不是真正的数组类型。t.is_a?(System::DateTime[])也将返回 true。

我认为这里发生的事情是 IronRuby 将方括号视为空的泛型类型指示符(因为创建泛型类型是使用相同的语法完成的,例如System::Collections::Generic::List[String].new)。

正确的做法如下:

IronRuby 0.9.3.0 on .NET 2.0.50727.4927
Copyright (c) Microsoft Corporation. All rights reserved.

>>> t = System::DateTime.new
=> 01/01/0001 00:00:00
>>> t.is_a? System::Array.of(System::Object)
=> false
于 2009-12-03T11:22:21.140 回答
1

使用[]泛型类型是 Ruby 的合理语法,但您看到的当前行为([]在非泛型类型上使用)是一个开放的错误:http: //ironruby.codeplex.com/WorkItem/View.aspx ?WorkItemId=3355

在 Ruby 中,方括号仅用于索引数组,而不是定义类型数组……因为不需要定义 Ruby 数组的类型。Ruby 允许[]对任何对象重载任何方法,而 Ruby 的Class对象对 没有含义[],因此我们定义[]为获取泛型类型。请记住,该of方法是首选方法;[]实际上只是为了方便和与 IronPython 类似的语法。

于 2009-12-29T04:17:36.283 回答