`

Ruby中将数组转化成hash

    博客分类:
  • ruby
阅读更多
ruby可以将元素数量为偶数的数组转成hash,但是元素数是奇数的则会出现错误
>> arr = [1,2,3,3]
=> [1, 2, 3, 3]
>> Hash[*(arr)]
=> {1=>2, 3=>3}
>> arr = [1,2,3,3,6]
=> [1, 2, 3, 3, 6]
>> Hash[*(arr)]
ArgumentError: odd number of arguments for Hash
	from (irb):11:in `[]'
	from (irb):11
分享到:
评论
Global site tag (gtag.js) - Google Analytics