tags:

views:

79

answers:

2

Can anybody explain why DateTime in IronRuby is Object[] sample code

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
+1  A: 

Because System::Object[] is not really an array type. *t.is_a?(System::DateTime[])* will return true as well.

I think that what happens here, is that IronRuby considers the square brackets as empty generic type indicators (because creating a generic type is done with the same syntax, for example, System::Collections::Generic::List[String].new).

The right way to do so is as follows:

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
Shay Friedman
it is a very weird, square bracket always used by ruby's array syntax. Why mix?Anyway thanks a lot.
Sergey Mirvoda
+1  A: 

Using [] for generic types is a justifiable syntax for Ruby, but the current behavior you see (using [] on a non-generic type) is a open bug: http://ironruby.codeplex.com/WorkItem/View.aspx?WorkItemId=3355

In Ruby, square brackets are only used for indexing into an array, not define a type-array ... as there is no need for defining the type of a Ruby array. Ruby allows overloading whatever [] means against any object, and Ruby's Class object doesn't have a meaning for [], so we've defined [] to mean getting a generic type. Keep in mind that the of method is the preferred method to use; [] is really only there for convenience and similar syntax with IronPython.

Jimmy Schementi