[Ironruby-core] 2d array

Orion Edwards orion.edwards at gmail.com
Sun Nov 29 20:22:25 EST 2009


... 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>


More information about the Ironruby-core mailing list