動態創建多維散列我是誰試圖獲得在Ruby中的一些精通PHP開發人員。我現在正在削減我的一個項目是一個源代碼審計工具,它可以掃描webapp文件,查找幾種網絡編程語言中潛在的危險功能。當找到匹配時,腳本保存在一個poi
(點利息)類顯示器後面的相關信息。在Ruby中
這個類中的一個實例會是這個樣子(在YAML建模):
poi:
file_type: "php"
file: "the-scanned-file.php"
line_number: 100
match: "eval()"
snippet: "echo eval()"
展出,我想整理這些景點,像這樣:
- file_type
-- file
--- match (the searched payload)
因此在演示之前,我試圖將一個平坦的poi
對象數組構造成一個鏡像上述結構的散列。這將允許我簡單地遍歷散列中的項目以產生期望的屏幕上組織。 (或者至少,計劃是這樣的。)
而現在,我的問題:我應該怎麼做,在紅寶石?
在PHP中,我可以做這樣的事情真的很容易:
<?php
$sorted_pois = array();
foreach($points_of_interest as $point){
$sorted_pois[$point->file_type][$point->file][$point->match][] = $point;
}
?>
我試着從PHP這個想法轉換到Ruby這樣的,但無濟於事:
sorted_pois = {}
@points_of_interest.each_with_index do |point, index|
sorted_pois[point.file_type.to_sym][point.file.to_sym][point.match.to_sym].push point
end
我「已經在這呆了幾個小時,我有種撞我的頭在這一點上牆,所以大概是我的方式關閉基地。在Ruby中處理這個問題的正確方法是什麼?
更新:
作爲參考,這是我所定義的精確的方法:
# sort the points of interest into a structured hash
def sort
sorted_pois = {}
@points_of_interest.each_with_index do |point, index|
sorted_pois[point.file_type.to_sym][point.file.to_sym][point.match.to_sym].push point
end
end
這是我收到的錯誤,當我運行的代碼:
./lib/models/vulnscanner.rb:63:in `sort': undefined method `[]' for nil:NilClass (NoMethodError)
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `each_with_index'
from ./lib/models/vulnscanner.rb:62:in `each'
from ./lib/models/vulnscanner.rb:62:in `each_with_index'
from ./lib/models/vulnscanner.rb:62:in `sort'
from ./webapp-vulnscan:69
62號線(你可以推斷出可能的)是這行特別是:
@points_of_interest.each_with_index do |point, index|
作爲附加參考,這裏是(的片段)@points_of_interest
看起來當轉換爲YAML,如:
- !ruby/object:PoI
file: models/couponkimoffer.php
file_type: php
group: :dangerous_functions
line_number: "472"
match: `
snippet: ORDER BY `created_at` DESC
- !ruby/object:PoI
file: models/couponkimoffer.php
file_type: php
group: :dangerous_functions
line_number: "818"
match: `
snippet: WHERE `company_slug` = '$company_slug'
- !ruby/object:PoI
file: models/couponkimoffer.php
file_type: php
group: :dangerous_functions
line_number: "819"
match: `
snippet: ORDER BY `created_at` DESC
這有什麼錯你有?它會導致錯誤還是輸出不是您所期望的?另外,提供樣本輸入/輸出也很有幫助。 – 2012-04-20 20:36:53
@AndrewMarshall,感謝您的關注。我剛更新了這個問題。 – 2012-04-20 20:47:57