I have some C# code which is expecting a 2d array.
public static class Foo
{
public static int Bar(object[,] inArray)
{ ... }
}
Is there any way that I can call this from IronRuby? I can''t figure out
how
to create the 2d array?
I have a feeling this has been asked before, but I can''t find any info
about
it...
Cheers, Orion
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/ironruby-core/attachments/20091130/64fb9b23/attachment-0001.html>
You can create an array using the BCL by calling Array.CreateInstance. From C#,
it would look like this:
Array.CreateInstance(typeof(object), 5, 6)); // Equivalent to new object[5,6]
I don''t have IronRuby on this machine and don''t want to guess
at the right syntax.
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at
rubyforge.org] On Behalf Of Orion Edwards
Sent: Sunday, November 29, 2009 4:41 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] 2d array
I have some C# code which is expecting a 2d array.
public static class Foo
{
public static int Bar(object[,] inArray)
{ ... }
}
Is there any way that I can call this from IronRuby? I can''t figure out
how to create the 2d array?
I have a feeling this has been asked before, but I can''t find any info
about it...
Cheers, Orion
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/ironruby-core/attachments/20091130/5590ef38/attachment.html>
This worked for me: System::Array.CreateInstance(System::Object.to_clr_type, 5, 6) Getting and setting works using x[1,2] as you''d expect Cheers! On Mon, Nov 30, 2009 at 1:44 PM, Curt Hagenlocher <curth at microsoft.com>wrote:> You can create an array using the BCL by calling Array.CreateInstance. > >From C#, it would look like this: > > > > Array.CreateInstance(typeof(object), 5, 6)); // Equivalent to new > object[5,6] > > > > I don''t have IronRuby on this machine and don''t want to guess at the right > syntax. > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091130/99e893b2/attachment.html>
... And here''s a convenient helper
class Array
def to_2d_a(ruby_type)
sub_count = self.length
max_sub_len = self.map{ |a| a.length }.max
ar = System::Array.CreateInstance(ruby_type.to_clr_type, sub_count,
max_sub_len)
self.each_with_index do |sub, idx|
sub.each_with_index do |item, sub_idx|
ar[idx, sub_idx] = item
end
end
ar
end
used like this
[[1,2],[3,4,5],[6,7]].to_2d_a(System::Int32)
:-)
On Mon, Nov 30, 2009 at 1:44 PM, Curt Hagenlocher <curth at
microsoft.com>wrote:>
>> You can create an array using the BCL by calling Array.CreateInstance.
>> >From C#, it would look like this:
>>
>>
>>
>> Array.CreateInstance(typeof(object), 5, 6)); // Equivalent to new
>> object[5,6]
>>
>>
>>
>> I don''t have IronRuby on this machine and don''t want
to guess at the right
>> syntax.
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://rubyforge.org/pipermail/ironruby-core/attachments/20091130/979a87a0/attachment.html>