2010-01-03 111 views
1

我試圖理解這個代碼片段:解釋紅寶石代碼片段

while row = input.gets 
    row.strip! 
    next if row.empty? 

    valuesplit = row.split("---") 
    a, b = valuesplit[1..2] 
    unless a == b 
    $hash1[a] ||= {} <--------------What is this line doing? How is the whole loop 
    $hash1[a][b] = true    being traversed? 

    if $hash1[b] && $hash1[b][a] <-----Can you please describe this if() loop 
     $hash2[a] ||= [] 
     $hash2[a] << b 
     $hash2[b] ||= [] 
     $hash2[b] << a 
    end 
    end 
end 

注:$ HASH1 = {} $ HASH2 = {}

謝謝!

UPDATE

輸入:

junkdata1 value1 value2 
junkdata2 value3 value4 
junkdata3 value5 value6 

等。

也用註釋更新了代碼行。

+0

有什麼不明白嗎? – JRL 2010-01-03 07:38:47

+0

我剛開始使用java開發,我需要了解這個ruby代碼,以便我可以在java上實現相同的代碼。如果某些身體有時間描述線條,我將非常感謝!我只是不能理解代碼! – zengr 2010-01-03 07:42:13

+0

我們能否獲得更多信息?這段代碼的輸入數據示例?很容易理解每​​一行代碼的功能,但是如果沒有更多的信息,很難確定整個代碼塊的目的是什麼。 – coderjoe 2010-01-03 07:52:15

回答

12
# loops by reading in every line of the input 
# (input can be a file or another I/O object) 
# every line is stored successively in a variable 
# called "row" 
while row = input.gets 

    # removes leading and trailing whitespace from 
    # the string that is stored in the "row" variable 
    row.strip! 

    # if the string is empty, continue to the next 
    # line (go back to beginning of loop) 
    next if row.empty? 

    # split the string into an array of substrings 
    # based on the "---" delimiter 
    valuesplit = row.split("---") 

    # assign the second substring in the valuesplit 
    # array to a variable called a, and the third to 
    # a variable called b 
    a, b = valuesplit[1..2] 

    # if a and b are different 
    unless a == b 

    # initialize the hash1 dictionary's a entry 
    # to an empty sub-dictionary if it is null 
    $hash1[a] ||= {} 

    # in the hash1 dictionary, set a's entry 
    # to a dictionary that has b as the entry 
    # and true as the value 
    $hash1[a][b] = true 

    # if the value for the b entry in the hash1 
    # dictionary is true (not false or null) AND the value for a's 
    # entry of the dictionary found at the b 
    # entry of the hash1 dictionary is true 
    if $hash1[b] && $hash1[b][a] 

     # initialize the hash2 dictionary's a entry 
     # to an empty arraylist if it null or false 
     $hash2[a] ||= [] 

     # add b to this arraylist 
     $hash2[a] << b 

     # initialize the hash2 dictionary's b entry 
     # to an empty arraylist if it null or false 
     $hash2[b] ||= [] 

     # add a to this arraylist 
     $hash2[b] << a 
    end # end of the if $hash1[b]... statement 
    end # end of the unless a == b statement 
end # end of the gets loop 
+1

令人驚歎的說明!謝謝JRL和coderjoe! – zengr 2010-01-03 20:10:56

5

我還是覺得這個問題有點含糊。您還應該注意到我忽略了您的示例數據。根據您的示例數據,$ hash1和$ hash2的結果都是空的哈希值。

關於第一個問題:

$hash1[a] ||= {} 

以上是兩件事情結合 首先是一個索引,我會假設你熟悉的哈希值。 第二種是有條件的分配。舉例來說:

blah ||= 1 

上面說的是,只要blah爲零,就將值賦值給blah。如果blah不是零,那麼分配不會執行。

對於if語句,我們需要一些背景:

if $hash1[b] && $hash1[b][a] #if the pair exists reversed 
    $hash2[a] ||= []   #initialize the array for the second 
    $hash2[a] << b    #append the third to the second's array 
    $hash2[b] ||= []   #likewise for the reverse 
    $hash2[b] << a 
end 

如果我們假設$ HASH1和$ HASH2初始值{}爲你注意,如果我們假設輸入是一---的值delimted系列,然後給出下面的數據集:

foo---b---c 
foo---c---a 
foo---a---b 
foo---b---a 
foo---a---d 
foo---d---a 

的$ HASH1的價值將是:

{"a"=>{"b"=>true, "d"=>true}, "b"=>{"a"=>true, "c"=>true}, "c"=>{"a"=>true}, "d"=>{"a"=>true}} 

其中$ HASH2是:

{"a"=>["b", "d"], "b"=>["a"], "d"=>["a"]} 

鑑於此,我可以讓一個受過教育的猜測的代碼塊產生關係的字典。在上面,$ hash1列出了給定值是否指向其他值。一種真相測試。如果您想知道A是否提及B,您可以使用:

$hash1['a']['b'] 

如果結果爲真,那麼答案是肯定的。

$ hash2是一種雙向關係的字典。 如果選中:

$hash2['a'] 

你會發現所有到A指的事也指A.

乾杯的數組。

2
foo ||= bar 

這是

foo = foo || bar 

就像是速記在Java中的簡寫。這基本上意味着「設置FOO等於欄的默認值,如果foo是目前爲零,否則不要管它」(無結果爲假紅寶石)