2014-03-04 115 views
0

我真的喜從LRtHW學習,我卡住了....瞭解紅寶石堅硬方式#41

我有這樣的程序:

require 'open-uri' 

WORD_URL = "http://learncodethehardway.org/words.txt" 
WORDS = [] 

PHRASES = { 
    "class ### < ###\nend" => "Make a class named ### that is-a ###.", 
    "class ###\n\tdef initialize(@@@)\n\tend\nend" => "class ### has-a initialize that takes @@@ parameters.", 
    "class ###\n\tdef ***(@@@)\n\tend\nend" =>"class ### has-a function named *** that takes @@@ parameters.", 
    "*** = ###.new()" => "Set *** to an instance of class ###.", 
    "***.***(@@@)" => "From *** get the *** function, and call it with parameters @@@.", 
    "***.*** = '***'" => "From *** get the *** attribute and set it to '***'." 
} 

PHRASE_FIRST = ARGV[0] == "english" 

open(WORD_URL) do |f| 
    f.each_line {|word| WORDS.push(word.chomp)} 
end 

def craft_names(rand_words, snippet, pattern, caps=false) 
    names = snippet.scan(pattern).map do 
    word = rand_words.pop() 
    caps ? word.capitalize : word 
    end 

    return names * 2 
end 

def craft_params(rand_words,snippet,pattern) 
    names = (0...snippet.scan(pattern).length).map do 
    param_count = rand(3) + 1 
    params = (0...param_count).map {|x| rand_words.pop()} 
    params.join(', ') 
    end 

    return names * 2 
end 

def convert(snippet, phrase) 
    rand_words = WORDS.sort_by {rand} 
    class_names = craft_names(rand_words, snippet, /###/, caps=true) 
    other_names = craft_names(rand_words, snippet,/\*\*\*/) 
    param_names = craft_params(rand_words, snippet, /@@@/) 

    results = [] 

    for sentence in [snippet, phrase] 
    #fake class name, also copies sentence 
    result = sentence.gsub(/###/) {|x| class_names.pop} 
    #fake other names 
    result.gsub!(/\*\*\*/) {|x| other_names.pop} 
    #fake parameter list 
    result.gsub!(/@@@/) {|x| param_names.pop} 
    results.push(result) 
    end 

    return results 
end 

# keep going until they hit CTRL-D 
loop do 
    snippets = PHRASES.keys().sort_by { rand } 

    for snippet in snippets 
    phrase = PHRASES[snippet] 
    question, answer = convert(snippet, phrase) 

    if PHRASE_FIRST 
     question, answer = answer, question 
    end 

    print question, "\n\n> " 
    odp = gets.chomp 

    if odp == "exit" 
     exit(0) 
    end 

    #exit(0) unless STDIN.gets 
    puts "\nANSWER: %s\n\n" % answer 
    end 
end 

我明白大多數代碼,但我有一個問題:

for sentence in [snippet, phrase] 

我知道這是一個「for」循環,它創建了一個「句」變量,但如何循環知道,它需要一個鍵和散列值來查找「PHRASES」

我的第二個「牆」是:

question, answer = convert(snippet, phrase) 

它看起來就像創建和分配「問題」和「答案變量‘摘要‘和‘短語’參數轉換’與方法’...它又如何將一個「問題」分配給一個鍵並回答一個值。

我知道,這可能是很簡單,但對於現在的塊我的腦海:(

+0

首先讓我們從拼寫更正開始。太多了。 – Bala

回答

1

你對for循環的第一個問題:

看看for循環的定義在哪裏。它是在convert()方法中的,對嗎?並且convert()方法傳遞了兩個參數:一個是snippet和一個是phrase因此循環沒有在PHRASES哈希中尋找值,是提供它正在使用該方法的參數。

對於喲關於作業的第二個問題:

在Ruby中,我們可以做一些名爲「解構賦值」的事情。這意味着我們可以將一個數組分配給多個變量,並且每個變量將在數組中保存一個值。這就是你的程序中發生的事情。 convert()方法返回一個兩項數組,並且給數組中的每個項目提供一個名稱(問題和答案)。

這裏有一個解構賦值的另一個例子:

a, b, c = [1, 2, 3] 
a # => returns 1 
b # => returns 2 
C# returns 3 

在IRB試試這個,看看,如果你得到了它的竅門。如果我能澄清任何事情,或者我誤解了你的問題,請告訴我。你不應該問「簡單」的問題!

+0

非常感謝你:)這個破壞性的分配「剛剛落在我的記憶牆上。 – Kask