我想從一個數組的行中構建一個哈希。我可以用下面的代碼輕鬆完成。我從PHP來到Ruby,這是我的方式。在Ruby(或Rails)中有更好/正確的方法嗎?創建紅寶石哈希的更好方法?
def features_hash
features_hash = {}
product_features.each do |feature|
features_hash[feature.feature_id] = feature.value
end
features_hash
end
# {1 => 'Blue', 2 => 'Medium', 3 => 'Metal'}
甜!謝啦! RubyMine不喜歡這種語法,但它工作正常!是的,這種方法看起來很方便! –