这段代码填充了一个@options
哈希。values
是一个Array
包含零个或多个异质项的。如果您使用作为条目populate
的参数进行调用Hash
,它会使用您为每个条目指定的值来假定默认值。
def populate(*args)
args.each do |a|
values = nil
if (a.kind_of? Hash)
# Converts {:k => "v"} to `a = :k, values = "v"`
a, values = a.to_a.first
end
@options[:"#{a}"] ||= values ||= {}
end
end
我想做的是改变populate
它以递归方式填充@options
. 有一种特殊情况:如果要填充键的值是一个完全由(1)符号或(2)键为符号(或两者的某种组合)的哈希组成的数组,那么它们应该被视为子键而不是与该键关联的值,并且populate
应该递归地重新应用用于评估原始参数的相同逻辑。
这有点难以用语言表达,所以我写了一些测试用例。以下是一些测试用例和@options
之后的期望值:
populate :a
=> @options is {:a => {}}
populate :a => 42
=> @options is {:a => 42}
populate :a, :b, :c
=> @options is {:a => {}, :b => {}, :c => {}}
populate :a, :b => "apples", :c
=> @options is {:a => {}, :b => "apples", :c => {}}
populate :a => :b
=> @options is {:a => :b}
# Because [:b] is an Array consisting entirely of Symbols or
# Hashes whose keys are Symbols, we assume that :b is a subkey
# of @options[:a], rather than the value for @options[:a].
populate :a => [:b]
=> @options is {:a => {:b => {}}}
populate :a => [:b, :c => :d]
=> @options is {:a => {:b => {}, :c => :d}}
populate :a => [:a, :b, :c]
=> @options is {:a => {:a => {}, :b => {}, :c => {}}}
populate :a => [:a, :b, "c"]
=> @options is {:a => [:a, :b, "c"]}
populate :a => [:one], :b => [:two, :three => "four"]
=> @options is {:a => :one, :b => {:two => {}, :three => "four"}}
populate :a => [:one], :b => [:two => {:four => :five}, :three => "four"]
=> @options is {:a => :one,
:b => {
:two => {
:four => :five
}
},
:three => "four"
}
}
如果需要更改签名populate
以适应某种递归版本,这是可以接受的。理论上可能发生的嵌套数量没有限制。
关于我如何解决这个问题的任何想法?