# 2d array

**URL:** <https://www.ruby-forum.com/t/2d-array/179594>\
**Category:** IronRuby\
**Created:** [November 30, 2009, 1:40am UTC](https://www.ruby-forum.com/t/2d-array/179594 "2009-11-30T01:40:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [November 30, 2009, 1:40am UTC](https://www.ruby-forum.com/t/2d-array/179594/1 "2009-11-30T01:40:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [November 30, 2009, 1:52am UTC](https://www.ruby-forum.com/t/2d-array/179594/2 "2009-11-30T01:52:34Z")

</div>

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: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Orion E.  
Sent: Sunday, November 29, 2009 4:41 PM  
To: removed\_email\_address@domain.invalid  
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

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [November 30, 2009, 2:18am UTC](https://www.ruby-forum.com/t/2d-array/179594/3 "2009-11-30T02:18:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Orion\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/65b543/32.png) [@Orion\_E](https://www.ruby-forum.com/u/Orion_E)\
**Post date:** [November 30, 2009, 2:23am UTC](https://www.ruby-forum.com/t/2d-array/179594/4 "2009-11-30T02:23:00Z")

</div>

… 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)

🙂
