2017-05-24 203 views
-1

我试图运行从一本书,我从学习Ruby程序,但我一直不管我如何努力修复它红宝石参数错误

ex41.rb:70:in `convert': wrong number of arguments (0 for 2) (ArgumentError) 
    from ex41.rb:117:in `block (2 levels) in <main>' 
    from ex41.rb:113:in `each' 
    from ex41.rb:113:in `block in <main>' 
    from ex41.rb:108:in `loop' 
    from ex41.rb:108:in `<main>' 

我明白收到此错误我有错误的参数数量,但事情是我无法找到我错误的地方,有人可以向我解释这个吗?为了方便起见,我把XXX(作为一个数字)放在引起我烦恼的线上。谢谢。

# calls external file from web 
require 'open-uri' 

# assigns WORD_URL to website 
WORD_URL = "http://learncodethehardway.org/words.txt" 
# creates empty array WORDS 
WORDS =[] 

# creates hash PHRASES 
PHRASES = { 
    "class ### < ### \nend" => 
    "Make a class named ### that is-a ###.", 
    # my assumption is that @@@ ### and the like will sb in words, lets find 
    out 
    "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 equal to '***'." 
} 

# creates variable for first argument if argument is english 
PHRASE_FIRST = ARGV[0] == "english" 

# opens WORD_URL function and creates function f 
open(WORD_URL) {|f| 
    # for every line in f chomps break character and pushes out word to f 
    f.each_line {|word| WORDS.push(word.chomp)} 
} 

# defines function craft_names with variables rand_words, snippet and 
pattern, and assigns caps value to false 
def craft_names(rand_words, snippet, pattern, caps=false) 
    # assigns vriable names to snippet.scan on pattern maps and does the 
following 
function 
    names = snippet.scan(pattern).map do 
    # assigns word to rand_words that have popped off the end 
    word = rand_words.pop() 
    # Guessing again, maybe capitalizes the first word 
    caps ? word.capitalize : word 
    # ends do 
    end 

    # returns names twice 
    return names * 2 
    # ends craft_names function 
end 

    # defines function craft_params with variables rand_words snippet, and 
     pattern 
def craft_params(rand_words, snippet, pattern) 
# assigns names to action scan with variable pattern with action 
# length on beginning part of array to snippet and runs the following 
# function using map 
    names = (0...snippet.scan(pattern).length).map do 
    # assigns variable param_count to an action that takes a random 3 and adds one to it 
    param_count = rand(3) + 1 
    # assigns variable params to one long ass action that maps 0 to param_count 
value and pops out the last word 
    params = (0...param_count).map { |x| rand_words.pop() } 
    # joins params list, (I'm sure its a list) with string , 
    params.join(", ") 
    # ends names for loop 
    end 

    # returns names twice 
    return names * 2 
# ends craft_params function 
end 

# defines convert function with variables snippet and phrase 
(LINE 70) def convert(snippet, phrase) 
    # sords words randomly and assigns words to rand_words 
    rand_words = WORDS.sort_by {rand} 
    # assigns class names to function craft mnames on rand_words, snippet /###/, and caps=true 
    class_names = craft_names(rand_words, snippet, /###/, caps=true) 
    # assigns other_names to craft_names with variables rand_words, snippet, and /\*\*\*/ 
    other_names = craft_names(rand_words, snippet, /\*\*\*/) 
    # assigns param_names to craft_params with rand_words, snippet, and @@@ as variables 
    param_names = craft_params(rand_words, snippet, /@@@/) 

    # assigns empty array results 
    results = [] 


    # on all variables snippet and phrase matchups perform the function 
sentence 
[snippet, phrase].each do |sentence| 

    # fake class names, also copies sentence 
    result = sentence.gsub(/###/) {|x| param_names.pop} 

    # fake other names 
    result.gsub!(/\*\*\*/) 

    # fake parameter lists 
    result.gsub!(/@@@/) 

    # returns result 
    results.push(result) 
    # ends function 
    end 

    # returns results 
    return results 
    # ends convert function 
end 

# keep going until they hit ctrl-d 
# continual loop 
(LINE 108)loop do 
    # assigns variable snippets to Prases.keys and sorts randomly 
    snippets = PHRASES.keys().sort_by {rand} 

# for snippet in snippets 
    (LINE113) snippets.each do |snippet| 
    # assigns PHRASES on snippet to phrase 
    phrase = PHRASES[snippet] 
    # asssigns question and answer to converted snippet and phrase 
    #respectively 
    (LINE117)question, answer = convert[snippet, phrase] 

    # if values in phrase firs are equal to answer, question 
    if PHRASE_FIRST 
     # question, answer = answer, question 
     question, answer = answer, question 
    # ends if 
    end 
    # prints question, two line breaks, and > without line break 
    print question, "\n\n> " 

    # closes program unless input 
    exit(0) unless $stdin.gets 

    # prints line break ANSWER: answer line break line break 
     puts "\nANSWER: %s\n\n" 

    # ends unless 
    end 
    # ends loop 
end 

回答

3

TL; DR - 当你写

convert[snippet, phrase] 

使用的convert(snippet, phrase)代替convert[snippet, phrase]


...它等同于:

convert()[snippet, phrase] 

添加空间:

convert [snippet, phrase] 

...将调用使用阵列的方法:

convert([snippet, phrase]) 

实际调用的方法有两个参数,用途:

convert(snippet, phrase) 

convert snippet, phrase