2011-02-04 57 views
1

我想从IronRuby脚本调用一个C#方法来产生Ruby散列。我试过字典,但保持原样。.Net对象对应于IronRuby散列

public Dictionary<string, string> GetHash() 
{ 
    Dictionary<string, string> hash = new Dictionary<string, string>(); 
    hash.Add("a", "1"); 
    hash.Add("b", "2"); 
    return hash; 
} 

我希望能够用它在我的IronRuby脚本作为哈希

myHash = scopeObj.GetHash() 
myHash.each{ |k,v| 
    print("#{k}=#{v}") 
} 

其结果是:

[a, 1]= 
[b, 2]= 

回答

1

它不喜欢的工作因为.NET字典中的项目是KeyValuePair实例。

您可以变通方法,很容易地与转换一行代码:

d = scopeObj.get_hash 
h = Hash[*d.collect { |x| [x.key,x.value] }.flatten] 
h.each { |k,v| puts k,v }