I want to call a 开发者_JS百科C# method from an IronRuby script that results in a Ruby hash. I tried Dictionary but that stays as is.
public Dictionary<string, string> GetHash()
{
Dictionary<string, string> hash = new Dictionary<string, string>();
hash.Add("a", "1");
hash.Add("b", "2");
return hash;
}
I'd like to be able to use it in my IronRuby script as a hash
myHash = scopeObj.GetHash()
myHash.each{ |k,v|
print("#{k}=#{v}")
}
The results of which are:
[a, 1]=
[b, 2]=
It doesn't work like that since the items in a .NET dictionary are KeyValuePair instances.
You can workaround that pretty easily, with a single line of conversion code:
d = scopeObj.get_hash
h = Hash[*d.collect { |x| [x.key,x.value] }.flatten]
h.each { |k,v| puts k,v }
精彩评论