我正在使用 Ruby on Rails 3.2.2。我有以下情况:
# hash_params.class
# => Hash
# hash_params.inspect
# => { :key1 => :value1, :key2 => value2, ... => ... }
#
def self.method_1(hash_params)
hash_params.each do { |hash_param| self.method_2(hash_param) }
end
# param.class
# => Array
# param.inspect
# => [:key1, value1] # or '[:key2, value2]' or '[..., ...]', depending on cases.
#
def self.method_2(param)
logger.debug "Key => #{param[0])"
logger.debug "Value => #{param[1])"
end
给定上面代码中注释掉的输出,当我method_1
在记录器文件中运行 then 时,我有以下内容:
Key => :key1
Value => :value1
Key => :key2
Value => :value2
Key => ...
Value => ...
我想将param
变量method_2
视为键/值对(而不是数组),例如通过制作如下内容
def self.method_2(param)
param do |key, value| # Note: This code line doesn't work. It is just a sample code to clarify the question.
logger.debug "Key => #{key.inspect)"
logger.debug "Value => #{value.inspect)"
end
end
? 是否可以?如果是这样,怎么做?你有什么建议?