【发布时间】:2017-06-21 20:56:22
【问题描述】:
如果我有一个如下所示的数组:
['one', { 'two' => 'three' }, 'four']
我怎样才能得到它:
['one', 'two', 'three', 'four']
.flatten 和 .flat_map 之类的东西不会删除内部哈希。
【问题讨论】:
-
['one', { 'two' => 'three' }, 'four'].flat_map { |el| el.is_a?(Hash) ? el.to_a : el }.flatten -
不健壮,只是为了好玩:
['one', { 'two' => 'three' }, 'four'].to_s.scan(/[a-z]+/)
标签: arrays ruby ruby-on-rails-4 hash