【问题标题】:IronRuby is_a with DateTimeIronRuby is_a 与 DateTime
【发布时间】:2009-12-03 10:38:12
【问题描述】:

谁能解释为什么 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

【问题讨论】:

    标签: ironruby dynamic-language-runtime


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      我认为这里发生的事情是 IronRuby 将方括号视为空的泛型类型指示符(因为创建泛型类型是使用相同的语法完成的,例如,System::Collections::Generic::列表[字符串].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
      

      【讨论】:

      • 这是一个非常奇怪的方括号,一直被 ruby​​ 的数组语法使用。为什么混合?无论如何,非常感谢。
      猜你喜欢
      • 1970-01-01
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多