红宝石 |数组 to_h()函数
Array#to_h() : to_h()是一个 Array 类方法,它返回将 ary 解释为 [key, value] 对数组的结果。
Syntax: Array.to_h()
Parameter: Array
Return: the result of interpreting ary as an array of [key, value] pairs.
示例 #1:
# Ruby code for to_h() method
# declaring array
a = [[:foo, :bar], [1, 2]]
# to_h method example
puts "to_h() method form : #{a.to_h()}\n\n"
输出 :
to_h() method form : {:foo=>:bar, 1=>2}
示例 #2:
# Ruby code for to_h() method
# declaring array
a = [[:geeks, :geeks], [1, 2]]
# to_h method example
puts "to_h() method form : #{a.to_h{|s| [s.ord, s]}}\n\n"
输出 :
to_h() method form : {:geeks=>:geeks, 1=>2}