理解Ruby代码?

时间:2012-05-02 01:23:30

标签: ruby twitter hashtag

我想知道是否有人可以帮我理解下面的Ruby代码?我对Ruby编程很陌生,并且无法理解每个函数的含义。

当我使用我的推特用户名和密码作为参数运行时,我获得了一些twitter源样本。我需要对此代码执行哪些操作才能显示主题标签?

我正在尝试每隔30秒收集一次主题标签,然后从最少到最多出现的主题标签进行排序。

不是寻找解决方案,而是寻求创意。谢谢!

require 'eventmachine'
require 'em-http'
require 'json'

usage = "#{$0} <user> <password>"
abort usage unless user = ARGV.shift
abort usage unless password = ARGV.shift

url = 'https://stream.twitter.com/1/statuses/sample.json'

def handle_tweet(tweet)
  return unless tweet['text']
  puts "#{tweet['user']['screen_name']}: #{tweet['text']}"
end

EventMachine.run do
  http = EventMachine::HttpRequest.new(url).get :head => { 'Authorization' => [ user, password ] }

  buffer = ""

  http.stream do |chunk|
    buffer += chunk
    while line = buffer.slice!(/.+\r?\n/)
      handle_tweet JSON.parse(line)
    end
  end
end

2 个答案:

答案 0 :(得分:1)

puts "#{tweet['user']['screen_name']}: #{tweet['text']}"

该行显示用户名,后跟推文内容。

让我们退后一步。

哈希标签出现在推文的内容中 - 这意味着它们在推文内部['text']。哈希标记始终采用#的形式,后跟一堆非空格字符。用正则表达式抓住这个很容易。 Ruby的核心API通过String#scan促进了这一点。例如:

"twitter is short #foo yawn #bar".scan(/\#\w+/) # => ["#foo", "#bar"]

你想要的是这样的:

def handle_tweet(tweet)
  return unless tweet['text']
  # puts "#{tweet['user']['screen_name']}: #{tweet['text']}" # OLD
  puts tweet['text'].scan(/\#\w+/).to_s
end

tweet ['text'] .scan(/#\ w + /)是一个字符串数组。您可以使用该阵列执行任何操作。假设您是Ruby新手,并希望将哈希标签打印到控制台,这里有一个关于使用puts打印数组的简短说明:

puts array      # => "#foo\n#bar"
puts array.to_s # => '["#foo", "#bar"]'

答案 1 :(得分:0)

#Load Libraries
require 'eventmachine'
require 'em-http'
require 'json'


# Looks like this section assumes you're calling this from commandline.
usage = "#{$0} <user> <password>"  # $0 returns the name of the program
abort usage unless user = ARGV.shift  # Return first argument passed when program called
abort usage unless password = ARGV.shift

# The URL
url = 'https://stream.twitter.com/1/statuses/sample.json'

# method which, when called later, prints out the tweets
def handle_tweet(tweet)
  return unless tweet['text']  # Ensures tweet object has 'text' property
  puts "#{tweet['user']['screen_name']}: #{tweet['text']}"  # write the result
end

# Create an HTTP request obj to URL above with user authorization
EventMachine.run do
  http = EventMachine::HttpRequest.new(url).get :head => { 'Authorization' => [ user, password ] }

  # Initiate an empty string for the buffer
  buffer = ""

  # Read the stream by line
  http.stream do |chunk|
    buffer += chunk
    while line = buffer.slice!(/.+\r?\n/)  # cut each line at newline
      handle_tweet JSON.parse(line)   # send each tweet object to handle_tweet method
    end
  end
end

这是源代码正在做的注释版本。如果你只想要标签,你需要将handle_tweet重写为:

handle_tweet(tweet)
  tweet.scan(/#\w/) do |tag|
    puts tag
  end
end