我在下面有以下函数,它通常会吐出一个 URL,例如 path.com/p/12345

有时,当一条推文在推文之前包含一个冒号时,例如



该函数将返回:

personName:
path.com/p/12345

我的功能:
$a = 10

def grabTweets()
  tweet = Twitter.search("[pic] "+" path.com/p/", :rpp => $a, :result_type => "recent").map do |status|
    tweet = "#{status.text}" #class = string
    urls = URI::extract(tweet) #returns an array of strings
  end
end

我的目标是在 URL 之前找到任何带有冒号的推文,并从循环中删除该结果,这样它就不会返回到创建的数组中。

最佳答案

您只能选择 HTTP URL:

URI.extract("RT: Something http://path.com/p/123")
  # => ["RT:", "http://path.com/p/123"]

URI.extract("RT: Something http://path.com/p/123", "http")
  # => ["http://path.com/p/123"]

你的方法也可以清理很多,你有很多多余的局部变量:
def grabTweets
  Twitter.search("[pic] "+" path.com/p/", :rpp => $a, :result_type => "recent").map do |status|
    URI.extract(status.text, "http")
  end
end

我还想强烈反对您使用全局变量 ( $a )。

10-07 19:25
查看更多