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

Introduction to Twitter's API Forum

View Course » View Exercise

277 points
51a9e192af581aba1e004894_789088052
Submitted by
Bin Diya
over 4 years ago

Someone help wit ex.4

Someone help wit ex.4
print ["text"] or puts ["text"] doesn't work as wel


6 votes

permalink

Try this:

 tweets.each do |tweet|
   puts tweet['text']
 end

It appears that the object tweets is an array of hashes. You'll want to access the hash key 'text' in each of the elements of the array tweets. Correct me if I'm wrong. This is quite confusing :)

BTW -- I couldn't get the exercise to pass with print.

852 points
Bc24425893e446ac1db249a844494420?s=140&d=retro
Submitted by
Philipp Antar
over 4 years ago

3 Comments

51a9e192af581aba1e004894_789088052 Bin Diya over 4 years ago

Thank u :) I completed

E2d32eefd26526d256d23df950771a59?s=140&d=retro Lu Xie over 4 years ago

Can you help to point out what my problem is? I tried with puts tweet["user"]["name"]+' - '+tweet["text"] but it always said "You did not generate the correct output. Did you print the username, followed by ' - ', followed by the Tweet text?"

Picture Shaikh Wasim over 3 years ago

Thanks.. I need to learn Ruby ;)


2 votes

permalink

def print_timeline(tweets)
tweets.each do |tweet|
puts tweet["text"]
end

end

277 points
51a9e192af581aba1e004894_789088052
Submitted by
Bin Diya
over 4 years ago


1 vote

permalink

Even if I changed the count to 10 and set the code like this:

baseurl = "https://api.twitter.com" 
path = "/1.1/statuses/user_timeline.json" 
query = URI.encode_www_form
( "screen_name" => "twitterapi", 
"count" => 10, ) 
address = URI("#{baseurl}#{path}?#{query}") 
request = Net::HTTP::Get.new address.request_uri 
# Print data about a list of Tweets 
def print_timeline(tweets) 
# ADD CODE TO ITERATE THROUGH EACH TWEET AND PRINT ITS TEXT 
  tweets.each do |tweet| 
     puts tweet["text"] 
  end 
end 

I still get the following error:
"Ooops, try again. You did not generate the appropriate number of output lines. Did you update the count parameter?"

Is there something wrong?

1692 points
51c348d8631fe90e9a0016a1_484802726
Submitted by
Ady Beleanu
about 3 years ago


0 votes

permalink

I passed the exercise.

For this exercise you should be able to open twitter with your browser, Twitter is inaccessible in some countries.

set count to 10 :
query = URI.encodewwwform(
"screen_name" => "twitterapi",
"count" => 10,

def print_timeline(tweets)
tweets.each do |tweet|
p tweet["text"]
end
end

335 points
3bf307a3aa6d9ac2722b7c6a1b5e2949?s=140&d=retro
Submitted by
ILU2014
about 3 years ago


0 votes

permalink

it worked with Phillip's approach. Thanks :)

1692 points
51c348d8631fe90e9a0016a1_484802726
Submitted by
Ady Beleanu
about 3 years ago