0

この例のコードを単語ごとに入力すると、次の構文エラー メッセージが表示されます。助けてください!!

https://github.com/visionmedia/google-search/blob/master/examples/web.rb

私のコード:

require "rubygems"
require "google-search"

def find_item uri, query
    search = Google::Search::Web.new do |search|
        search.query = query
        search.size = :large
        search.each_response {print "."; #stdout.flush}
    end
        search.find {|item| item.uri =~ uri}
end

def rank_for query
    print "%35s " % query
    if item = find_item(/vision\-media\.ca/, query)
        puts " #%d" % (item.index +1)
    else
        puts " Not found"
    end
end

rank_for "Victoria Web Training"
rank_for "Victoria Web School"
rank_for "Victoria Web Design"
rank_for "Victoria Drupal"
rank_for "Victoria Drupal Development"

エラーメッセージ:

Ruby Google Search:9: syntax error, unexpected keyword_end, expecting '}'
Ruby Google Search:11: syntax error, unexpected keyword_end, expecting '}'
Ruby Google Search:26: syntax error, unexpected $end, expecting '}'
4

3 に答える 3

2

うっかり 9 行目の残りの部分をコメントアウトしてしまいました。

search.each_response {print "."}

#Rubyの文字はコメントを表すことに注意してください。# つまり、同じ行内のインクルーシブの右側にあるものはすべてコメントと見なされ、Ruby コードとしてコンパイルされません。

print 'this ' +  'is ' + 'compiled'
#=> this is compiled

print 'this' # + 'is' +  'not'
#=> this

ブラケット{}表記は、ブロック内に含まれる単一の実行可能な行をカプセル化することに注意してください。ただし、実行しようとしているのは、2 つのコマンドを実行することです。このため、Ruby のblock表記法を使用する方が意味的に読みやすい場合があります。

search.each_response do
    print '.'
    STDOUT.flush
end
于 2013-10-15T02:05:25.867 に答える
-1

do ブロックの最後の行find_itemは次のとおりです。

search.each_response {print "."; #stdout.flush}

Ruby#の はコメントの開始を示します。行の残りの部分はコメントアウトしましたが、ブラケットを開く前ではありません{。閉じられていないことがエラーの原因です。

コードを正しくするには、グローバル stdout オブジェクトにアクセスするように変更#する必要があります。$

于 2013-10-15T02:05:48.603 に答える