Ruby语法糖:将两个数组合并成一个哈希

重构的需求

原来的丑×代码如下

subjects.each do |m|
  subject = {}
  subject[:cno] = m.children[1].text.slice(1..-1).strip
  subject[:name] = m.children[3].text.slice(1..-1).strip
  subject[:grade] = m.children[7].text.slice(1..-1).strip
  subject[:date] = m.children[9].text.slice(1..-1).strip
  subject[:point] = get_point(subject[:grade]).to_s
  @guide_score_list << subject
end

我本来想直接改成下面这样

@guide_score_list << {
  cno: m.children[1].text.slice(1..-1).strip,
  name: m.children[3].text.slice(1..-1).strip,
  grade: m.children[7].text.slice(1..-1).strip,
  date: m.children[9].text.slice(1..-1).strip,
  point: "大写的懵逼"
}

然后发现point的值要根据grade来计算,而这样写就只能写成下面这样,更加恶心。

get_point(m.children[7].text.slice(1..-1).strip).to_s

我觉得这段代码重复了好多遍,应该抽出来

m.children[i].text.slice(1..-1).strip

所以就想到了,能不能直接把两个数组元素一对一的,一个为key,一个为value,集合成Hash呢?

甜到掉牙的语法糖

keys = [:foo, :bar, :bat]
vals = [4, 5, 6]
# do something to get
{foo: 4, bar: 5, bat: 6}

于是这个风骚的方法就是

h = Hash[keys.zip vals]
# 其中 keys.zip vals  => [[:foo, 4], [:bar, 5], [:bat, 6]]
# 然后再用Hash::[]方法生成一个Hash

这个方案是我从Stack Overflow里查来的,回答的人原话是这样的

h = Hash[a.zip b]
...damn, I love Ruby.

所以代码改成了下面这样,又简洁,又高端,这是最骚的

subjects.each do |m|
  vals = [1, 3, 7, 9].map{ |i| m.children[i].text.slice(1..-1).strip }
  vals << get_point(vals[2]).to_s
  keys = [:cno, :name, :grade, :date, :point]
  @guide_score_list << Hash[keys.zip vals]
end

其他细节

数组等长的情况下,这么做很完美,如果不等长呢?
试一下就知道了

keys = [:foo, :bar, :bat]
vals = [4, 5, 6, 7]
keys.zip vals # => [[:foo, 4], [:bar, 5], [:bat, 6]]
vals = [4, 5]
keys.zip vals # => [[:foo, 4], [:bar, 5], [:bat, nil]]
kvs = keys.zip vals
[keys, vals].transpose == kvs # => keys和vals长度相同时为true,长度不同时transpose方法会报错
Hash[kvs] == kvs.to_h # => true

你可能感兴趣的:(Ruby语法糖:将两个数组合并成一个哈希)