Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7877309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:20:08+00:00 2026-06-03T03:20:08+00:00

I was wondering if anyone can help me understanding the Ruby code below? I’m

  • 0

I was wondering if anyone can help me understanding the Ruby code below? I’m pretty new to Ruby programming and having trouble understanding the meaning of each functions.

When I run this with my twitter username and password as parameter, I get a stream of twitter feed samples. What do I need to do with this code to only display the hashtags?

I’m trying to gather the hashtags every 30 seconds, then sort from least to most occurrences of the hashtags.

Not looking for solutions, but for ideas. Thanks!

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:20:10+00:00Added an answer on June 3, 2026 at 3:20 am
    puts "#{tweet['user']['screen_name']}: #{tweet['text']}"
    

    That line shows you a user name followed by the content of the tweet.

    Let’s take a step back for a sec.

    Hash tags appear inside the tweet’s content–this means they’re inside tweet[‘text’]. A hash tag always takes the form of a # followed by a bunch of non-space characters. That’s really easy to grab with a regex. Ruby’s core API facilitates that via String#scan. Example:

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

    What you want is something like this:

    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+/) is an array of strings. You can do whatever you want with that array. Supposing you’re new to Ruby and want to print the hash tags to the console, here’s a brief note about printing arrays with puts:

    puts array      # => "#foo\n#bar"
    puts array.to_s # => '["#foo", "#bar"]'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering if anyone can help me out with some Canvas. I'm pretty new
I'm having a little trouble switching views and wondering anyone can help. Basically the
I am having trouble with my game and was wondering if anyone can help?
I'm wondering if anyone with a better understanding of python and gae can help
Just wondering if anyone can help me in andriod app development. I'm new to
I was wondering if anyone can help me to get this code working...? <?php
Very new to Jquery and wondering if anyone can help me with the following
Hey, I'm having a strange issue. I'm wondering if anyone can help me make
I'm new to Objective C and was wondering if anyone can help me. I
I was wondering if anyone can help me write some code for the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.