This forum is now read-only. Please use our new forums at

Introduction to Twitter's API Forum

View Course » View Exercise

794 points
Submitted by
Alexander Leith-Ross
almost 3 years ago

Successfully passed, No tweets!

I managed to pass onto the next question with my code but don't seem to have received any Tweets from the timeline to read.

require 'rubygems'
require 'oauth'
require 'json'

baseurl = ""
path    = "/1.1/statuses/user_timeline.json"
query   = URI.encode_www_form(
    "screen_name" => "twitterapi",
    "count" => 10,
address = URI("#{baseurl}#{path}?#{query}")
request = address.request_uri

def print_timeline(tweets)
  tweets.each do |tweet| print_timeline


http             =, address.port
http.use_ssl     = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

consumer_key ||= "ENTER IN EXERCISE 1", ""
access_token ||= "ENTER IN EXERCISE 1", ""

request.oauth! http, consumer_key, access_token
response = http.request request

tweets = nil
if response.code == '200' then
  tweets = JSON.parse(response.body)

Taking a quick look at what I entered:

baseurl = ""
path    = "/1.1/statuses/user_timeline.json"
query   = URI.encode_www_form(
    "screen_name" => "twitterapi",
    "count" => 10,

Which seems to have been fine however the next bit is what I think is causing me the problem:

def print_timeline(tweets)
  tweets.each do |tweet| print_timeline


Which then displays my green tick as well as this message in the console:

(ruby):47: syntax error, unexpected $end, expecting keyword_end

Thank you very much in advance to whoever can help me out with this, greatly appreciated.

2 votes


Yep - problem is in your method. Your block isn't closed, it should be like this:

tweets.each do |tweet|

But your block is wrong too. Look what similar method returns:

irb(main):102:0> def my_method(arr)
irb(main):103:1>   arr.each { my_method }
irb(main):104:1> end
=> nil
irb(main):105:0> my_method([1,2])
ArgumentError: wrong number of arguments (0 for 1)

You call to print_timeline method without passing any argument.
Actually you need something like this:

tweets.each { |tweet| puts tweet['text'] }

We've got array of tweets passed to print_timeline method and then we just iterate through each of them.

715 points
Submitted by
Nikolai Volochaev
almost 3 years ago

1 Comment

Picture Liz Pullen almost 3 years ago

This resolved the problem for me. Thanks!