0

I am collecting tweets using a 'Twitter' gem, gem doc, github page. As it filters tweets according to a criteria, eg:

client.filter(locations: "-122.75,36.8,-121.75,37.8") do |tweet|
  puts tweet.text
end

A tweet object is produced from the stream;

(rdb:1) tweet.class
Twitter::Tweet

I can run tweet.methods to get the list of methods available but that includes methods not specific to the tweet api. It includes methods from Object etc. Since I would like to extract from this object all the tweet specific information to see it, is there a way to run all the methods from Class: Twitter::Tweet, from the list of 'Instance Method Summary' and 'Instance Attribute Summary' automatically without having to call each one individually? (I want to make a JSON of all the data in these tweet objects)

and can you explain what this means in terms of the oop paradigm in ruby- "Twitter::Tweet"? does it mean that the tweet object is part of the Twitter api and Tweet class?

Vass
  • 2,682
  • 13
  • 41
  • 60
  • possible duplicate of [List all methods for an object?](http://stackoverflow.com/questions/8595184/list-all-methods-for-an-object) – Tony Hopkinson Dec 27 '14 at 13:52
  • @TonyHopkinson I don't want to list, but run them. – Vass Dec 27 '14 at 13:53
  • Hmm that's a bit more involved, depends on the arguments, but at it's simplest. it's mytweet.methods.each{|m| mytweet.send(m)} – Tony Hopkinson Dec 27 '14 at 13:56
  • @TonyHopkinson, I think it might be working but get some errors of deprecation, geo_harvest.rb:12:in `block (2 levels) in
    ': [DEPRECATION] #favorites_count is deprecated. Use #favorite_count instead.
    – Vass Dec 27 '14 at 14:06
  • That's why I said a bit more involved. :) And why there isn't some built in ruby to do it. Too many ifs and buts. For instance method1 changes something so method2 fails. – Tony Hopkinson Dec 27 '14 at 14:09
  • One way would be to be to extend the Tweet and add a method that used a whitelist array of the method names you want to run... – Tony Hopkinson Dec 27 '14 at 14:12

1 Answers1

2

For the first question: you can do it in a little not obvious way. Just ask for own methods of the class:

methods = tweet.class.instance_methods(false)

false parameter is important here, it removes all methods of parent classes. And as a consequence you can run all the methods from the returned list. For example:

methods.each{|method_name| tweet.send(method_name)}

For the second question: Twitter means a main module for all gem related classes, it uses in order to not mess global namespace with twitter-only-related classes.

Tim Tonkonogov
  • 1,099
  • 10
  • 14
  • method_name not the symbol :method_name mate. An this will onlly word for methods with no arguments and ones where all the arguments have a default – Tony Hopkinson Dec 27 '14 at 14:02
  • Yes, you completely right about syntax. Will update it. About methods calling. Well... The task is formulated as `is there a way to run all the methods from Class: Twitter::Tweet, from the list of 'Instance Method Summary' and 'Instance Attribute Summary' automatically without having to call each one individually` what does not make sense for me. I just gave what he asked. – Tim Tonkonogov Dec 27 '14 at 14:13
  • Yeah he wants to skip deprecations now as well, I can't see a sensible way of doing this. – Tony Hopkinson Dec 27 '14 at 14:16