我正在研究一個腳本,它收集來自我的數據庫的所有註釋並將它們插入到散列中。我然後做一個收集有關意見Hash和分裂的意見分成兩個獨立的評論哈希(vid_comments & wall_comments)Ruby:使用數組
w_hash["comments"] << contender.profile.comments.where("created_at > DATE_SUB(NOW(), INTERVAL 1 DAY)")
w_hash["comments"].delete_if { |x| x.blank? }
w_hash["vid_comments"], w_hash["wall_comments"] = [], []
w_hash["comments"].each do |c|
if !c.media_id.nil?
w_hash["vid_comments"] << c
elsif c.parent_id == 0
w_hash["wall_comments"] << c
end
end
反正是有縮短的代碼?我對Ruby很新(PHP導入),所以請原諒我對我可能做錯的事情的無知。
編輯:添加代碼位來自@Mchl(下)..
考慮將您的問題移至http://codereview.stackexchange.com。 – maerics
謝謝!後移到codereview:http://codereview.stackexchange.com/questions/3987/ruby-working-with-arrays – dennismonsewicz