I'm trying to set an array property in a COM object using Ruby v1.93. Marshalling an array from COM into a Ruby array just works, but not in the other direction. How do I marshall a Ruby array into COM?
The property is contained in a .NET assembly:
namespace LibForRuby
{
public class MyClass
{
public MyClass()
{
MyInt = 1;
MyArray = new[] {2, 3};
}
public int MyInt { get; set; }
public int[] MyArray { get; set; }
}
}
My entire ruby script is:
require 'win32ole'
com_class = WIN32OLE.new('LibForRuby.MyClass')
puts 'Before:'
my_int = com_class.MyInt
puts my_int
my_array = com_class.MyArray
print my_array
puts
puts 'After:'
com_class.MyInt = 10
my_int = com_class.MyInt
puts my_int
com_class.MyArray = [20,30]
my_array = com_class.MyArray
print my_array
The output is:
C:\Ruby193\bin>test
Before:
1
[2, 3]
After:
10
C:/Ruby193/bin/test.rb:13:in `method_missing': (in setting property `MyArray': )
(WIN32OLERuntimeError)
OLE error code:0 in <Unknown>
<No Description>
HRESULT error code:0x80020005
Type mismatch.
from C:/Ruby193/bin/test.rb:13:in `<main>'
Try this:
Make sure to lookup the #_setproperty method at ruby-doc.org to see why your array is enclosed in an array and why the variant constant is inside an array.