所以,这就是我到目前为止所拥有的:
# @return [Array<Hash>] of variables ([Placeholders, actual variables])
def variables
# @return [Hash] of all variables.
# If actual variables are named teh same as the placeholders, the placeholders will be over written.
# This implementation of merging hashes of an array will work even if we add more elements to the
# variables array.
self.class.send(:define_method, "variables.flatten") do
return self.variables.inject{|placeholders, vars| placeholders.merge(vars || {})}
end
return [placeholder_variables, self.variable_data]
end
我希望能够做到:
my_object.variables # => return the Array<Hash>
但也能做到
my_object.variables.flatten # => return a single merged hash
我刚做的问题
def variables.flatten
是当我运行控制台时,我收到一条错误消息,说variables
没有定义(或者只是我之前放的任何东西.flatten
)
.flatten
有没有办法只在属性上定义我的自定义方法variables
?(正常<Array>.flatten
在这里不起作用,因为我需要合并哈希。)