开发者

What .Net object corresponds to an IronRuby hash

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 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜