0

Zed Shaw 对 Ruby 课程的介绍基本上完成了一半。但是,我在这个练习中遇到了障碍,他给了我一段代码并要求我修复它。很多错误是语法错误,但有些编码错误我不太明白。

我得到的错误如下:

ex26.rb:76:语法错误,意外 ')',期待 '='

ex26.rb:99: 语法错误,意外 $end,期待 ')'

编码:

# This function will break up words for
def break_words(stuff)
    words = stuff.split(' ')
    return words
end

# Sorts the words.
def sort_words(words)
    return sorted(words)
end

# Prints the first word after popping it off.
def puts_first_word(words)
    word = words.poop(0)
    puts word
end

# Prints the last word after popping it off.
def puts_last_word(words)
    word = words.pop(-1)
    puts word
end

# Takes in a full sentence and returns the sorted words.
def sort_sentence(sentence)
    words = break_words(sentence)
    return sort_words(words)
end

# Puts the first and last words of the sentence.
def puts_first_and_last(sentence)
    words = break_words(sentence)
    puts first_word(words)
    puts last_word(words)
end

# Sorts the words then prints the first and last one.
def puts_first_and_last_sorted(sentence)
    words = sort_sentence(sentence)
    puts first_word(words)
    puts last_word(words)
end


puts "Let's practice everything."
puts 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = <<POEM
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
POEM


puts "--------------"
puts poem
puts "--------------"

five = 10 - 2 + 3 - 6
puts "This should be five: %s" % five

def secret_formula(started)
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates
end

start_point = 10000
beans, jars, crates = secret_formula(start-point)

puts "With a starting point of: %d" % start_point
puts "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

puts "We can also do that this way:"
puts "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point)


sentence = "All good\tthings come to those who wait."

words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

puts first_word(words)
puts last_word(words)
puts first_word(sorted_words)
puts last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
print sorted_words

puts first_and_last(sentence)

puts first_and_last_sorted(sentence)
4

3 回答 3

2

看一下对secret_formulafirst 的调用;如果没有别的,你可能在其中一个中有错字。

其中一个错别字附近有一个论据puts;它包含在错误的字符中。

于 2011-10-20T15:06:12.103 回答
1

你还有:

# Prints the first word after popping it off.
def puts_first_word(words)
    word = words.poop(0)
    puts word
end

.poop()方法在哪里声明?

于 2011-10-20T15:12:34.000 回答
0

另外,对于句子的第一个词:

# Prints the first word after popping it off.
def puts_first_word(words)
    word = words.poop(0)
    puts word
end

不仅有错字,而且如您所见,.pop它用于获取句子中的最后一个单词。你能找到一个类似的函数来获取第一个单词吗?

(提示:我们在之前的练习中使用它)

于 2012-06-16T01:52:46.520 回答