2011-10-20 3 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;それ以外のものがない場合は、そのうちの1つにタイプミスがある可能性があります。

これらのタイプミスの1つは、putsの引数です。それは間違った文字で囲まれています。あなたも持っている

+0

あなたが正しいと思います。私の答えを削除しました。彼はまだそれを見ていないと思う;-) – Mischa

+0

@ミシャ私はあなたがそれを遠くに行く必要はないと思う;ただちょっと微調整してください:)私のことは "あまりにも難しい"かもしれませんが、それはいつも細い線です。 –

+0

ex26.rb:10: 'sort_words 'の中でmain:Object(NoMethodError)の定義されていないメソッド \t ex26.rb:88:'

'(これはどういう意味ですか?すでに) –

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は、文の最後の単語を取得するために使用されます。あなたはの最初にという言葉を得るために同様の関数を見つけることができますか?

(ヒント:私たちは前に運動中にそれを使用)

関連する問題