2014-03-04 66 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