加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – URI提取以冒号逃逸,以任何方式避免这种情况?

发布时间:2020-12-17 02:17:02 所属栏目:百科 来源:网络整理
导读:我在下面有以下功能,通常会吐出一个URL,例如path.com/p/12345. 有时,当推文在推文之前包含冒号时,例如 RT: Something path.com/p/123 该函数将返回: personName:path.com/p/12345 我的功能: $a = 10def grabTweets() tweet = Twitter.search("[pic] "+" pa
我在下面有以下功能,通常会吐出一个URL,例如path.com/p/12345.

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

RT: Something path.com/p/123

该函数将返回:

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网址:

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/",:result_type => "recent").map do |status|
    URI.extract(status.text,"http")
  end
end

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读