【问题标题】:how can i use ruby and twitter tweet from text file我如何使用文本文件中的 ruby​​ 和 twitter 推文
【发布时间】:2015-08-13 22:11:09
【问题描述】:

我正在尝试从文本文件中发送推文而不获取额外的符号。

我的推文如下所示: [“这是我在 twitter 上的推文示例”\n]

我想要得到的是: 这是我希望我的推文看起来像的示例。

我的代码:

def tweet
  file = File.open("tweets_from.txt","r")
  read = file.readlines.sample(1)
  client.update("#{read}").text
end

tweet

提前感谢您的帮助:)

【问题讨论】:

    标签: ruby file twitter readfile tweets


    【解决方案1】:

    改变就够了

    read = file.readlines.sample(1)
    

    到这里:

    read = file.readlines.sample.chomp
    

    更多关于这个方法(和许多其他!)在这里:Ruby String docs

    【讨论】:

    • @marriedjane875 很高兴为您提供帮助:)
    【解决方案2】:
    read = file.readlines.map(&:chomp).sample
    client.update(read).text
    

    【讨论】:

    • 在这里你可以对整个数组进行 chomp,而这并不是必需的。
    猜你喜欢
    • 2018-05-03
    • 2012-12-31
    • 1970-01-01
    • 2018-03-16
    • 2011-04-18
    • 2015-06-19
    • 2017-04-10
    • 2019-06-11
    • 2018-04-25
    相关资源
    最近更新 更多