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

请注意,括号{}表示法封装了块中包含的单个可执行行。但是,您要做的是执行两个命令。为此,使用 Ruby 的block符号可能在语义上更具可读性:

search.each_response do
    print '.'
    STDOUT.flush
end
于 2013-10-15T02:05:25.867 回答
0

而不是#stdout.flush,键入$stdout.flush

于 2013-10-15T02:27:12.220 回答
-1

do 块的最后一行find_item是:

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

#Ruby 中标记注释开始的地方。您已经注释掉了该行的其余部分,但不是在打开括号之前{。没有关闭它是您错误的根源。

为了使您的代码正确,您应该将 更改#$访问全局标准输出对象。

于 2013-10-15T02:05:48.603 回答