Orion_E
#1
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
Orion_E
#2
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: [email protected]
[mailto:[email protected]] On Behalf Of Orion E.
Sent: Sunday, November 29, 2009 4:41 PM
To: [email protected]
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
Orion_E
#3
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!
Orion_E
#4
… 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)
