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
? 出来ますか?もしそうなら、どのように?何についてアドバイスしますか?