在这个示例中,几乎已经逐字输入了代码,并收到了下面的语法错误消息。救命啊!!
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 '}'发布于 2013-10-15 02:05:25
您无意中将第9行的其余部分注释掉:
search.each_response {print "."}注意,#中的#字符表示注释;也就是说,#包含的右侧同一行的所有内容都被认为是注释,而不是编译为#代码。
print 'this ' + 'is ' + 'compiled'
#=> this is compiled
print 'this' # + 'is' + 'not'
#=> this请注意,括号{}符号封装了块中包含的单个可执行行。但是,您要做的是执行两个命令。为此,使用Ruby的block表示法可能更具有语义可读性:
search.each_response do
print '.'
STDOUT.flush
end发布于 2013-10-15 02:27:12
而不是#stdout.flush,输入$stdout.flush。
发布于 2013-10-15 02:05:48
find_item中do块的最后一行是:
search.each_response {print "."; #stdout.flush}Ruby中的#标志着注释的开始。您已经注释掉了行的其余部分,但在打开括号{之前没有注释。没有关闭它是您错误的根源。
为了使代码正确,应该将#更改为$以访问全局标准输出对象。
https://stackoverflow.com/questions/19372165
复制相似问题