Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
艰苦地学习Ruby_Ruby_Variables_Hash - Fatal编程技术网

艰苦地学习Ruby

艰苦地学习Ruby,ruby,variables,hash,Ruby,Variables,Hash,嗨,我在向LRtHW学习,我被卡住了 我有这样的计划: 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"

嗨,我在向LRtHW学习,我被卡住了

我有这样的计划:

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”循环,它创建了一个“句子”变量,但是循环如何知道它需要查找散列“短语”的键和值呢

我的第二道“墙”是:

它看起来像是用“snippet”和“phrase”参数创建“question”和“answer”变量并将其分配给“convert”方法……同样,它如何将“question”分配给键,将“answer”分配给值


我知道这可能很简单,但现在它挡住了我的思路:(

关于for循环的第一个问题:

查看for循环的定义位置。它位于convert()方法内部,对吗?convert()方法传递两个参数:一个
snippet
和一个
短语
。因此循环不是“查找”短语散列中的值,是您提供了它。您正在使用该方法的参数

关于作业的第二个问题:

a, b, c = [1, 2, 3]
a # => returns 1
b # => returns 2
c # returns 3
在Ruby中,我们可以执行一种称为“destructuring assignment”的操作。这意味着我们可以将一个数组分配给多个变量,每个变量将在数组中保留一个值。这就是您的程序中发生的情况。convert()方法返回一个两项数组,您可以指定一个名称(问答)添加到数组中的每个项

下面是另一个分解分配示例:

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

在IRB中尝试一下,看看你是否掌握了窍门。如果我能帮你澄清什么,或者我是否误解了你的问题,请告诉我。问“简单”的问题你永远不会感到不安!

首先让我们从拼写更正开始。有太多了。非常感谢:)这个破坏性的分类“刚刚落在我的记忆墙上。
a, b, c = [1, 2, 3]
a # => returns 1
b # => returns 2
c # returns 3