2011-10-20 88 views
0

我基本上已經完成了Zed Shaw對Ruby課程的介紹。然而,我在這個練習中遇到了一個障礙,他給了我一段代碼並要求我修復它。大部分錯誤都是語法錯誤,但是有些編碼錯誤是我無法理解的。是卡在Zed Shaw的紅寶石練習

我得到的錯誤如下:

ex26.rb:76:語法錯誤,意外 ')',希望 '='

ex26.rb:99:語法錯誤,意外$結束,預計 ')'

代碼:

# 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) 
+0

hey..point出來的行號76和99也 – rubyprince

+0

http://hyperboleandahalf.blogspot.com/2010/04/alot-is-better -than-you-at-everything.html –

回答

2

看看來電來首先是secret_formula;如果沒有別的,你可能會在其中一個輸入錯誤。

其中一個錯別字是puts的一個參數;它包含在錯誤的字符中。

+0

我想你是對的。刪除了我的答案。希望他還沒有看到它;-) – Mischa

+0

@Mischa我不認爲你需要去那麼遠;只是稍微調整一下:)礦可能*太「*硬」,它總是一條細線。 –

+0

ex26.rb:10:在'sort_words':未定義的方法'排序'爲主:對象(NoMethodError) \t from ex26.rb:88:in'

'(這個錯誤指的是什麼?我以爲我定義了它已經) –

1

你也有:

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

哪裏是.poop()方法聲明?

+0

是的,這應該是流行的感謝。 –

+0

+1有趣。 (哦彈出,更多字符鍵入) –

0

此外,對於句子的第一個字:

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

不僅是有一個錯字,但你可以看到,.pop用於獲取在句子中的最後一個字。你能找到一個類似的功能來獲得第一個這個詞嗎?

(提示:我們之前在運動中使用它)