0

I am using the twitter gem to interact with the twitter API.

My output for approach 1, below, is what you'd expect, the last 5 tweets containing the President's name.

client.search("to: Donald Trump", result_type: "recent").take(5).each do |tweet|
  puts tweet.text
end

With this second approach, though, storing client.search into an array and then displaying it, I don't get plain text.

trumptweets = client.search("to: Donald Trump", result_type:"recent").take(5)
puts trumptweets

Output: #<Twitter::Tweet:0x007fbbacb61060>
#<Twitter::Tweet:0x007fbbacb61010>
#<Twitter::Tweet:0x007fbbacb60f98>
#<Twitter::Tweet:0x007fbbacb60f70>
#<Twitter::Tweet:0x007fbbacb60ed0>

How do I a) convert the output from approach #2 to actual text or b) why does looping over a hash seem to be the only way to do this?

engineersmnky
  • 25,495
  • 2
  • 36
  • 52
db100
  • 1
  • 2
  • In the first version you're explicitly accessing the `.text`, in the second the whole object. It's not clear why you expected anything else. – jonrsharpe Aug 28 '17 at 19:56
  • Is there any way, without using a loop to parse, to only access the .text using the second method? – db100 Aug 28 '17 at 20:01
  • 3
    The shorthand usually looks like `some_array.map(&:text)` See https://stackoverflow.com/questions/1217088/what-does-mapname-mean-in-ruby – whodini9 Aug 28 '17 at 20:18
  • In the first case you're doing a loop, printing each Tweet text. In the second case, you got an array of Tweet objects. You need to loop through them. It can be done like @whodini9 said, with "map" or just a normal "each" loop. – valrog Aug 29 '17 at 09:16
  • Thx, very helpful. – db100 Sep 07 '17 at 01:35

0 Answers0