[英]In Ruby how to run each method of an object at a particular level in the hierarchy (not the methods it inherits)?
I am collecting tweets using a 'Twitter' gem, gem doc , github page . 我正在使用'Twitter'gem, gem doc和github页面收集推文。 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; 从流中产生一条
tweet
对象;
(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. 我可以运行
tweet.methods
以获得可用方法列表,但其中包括非特定于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? 它包括来自Object等的方法。由于我想从该对象中提取所有tweet特定信息以查看它,因此有一种方法可以从“实例方法摘要”列表中运行Class:Twitter :: Tweet中的所有方法。和“实例属性摘要”自动生成,而不必分别调用每个? (I want to make a JSON of all the data in these tweet objects)
(我想为这些tweet对象中的所有数据制作一个JSON)
and can you explain what this means in terms of the oop paradigm in ruby- "Twitter::Tweet"? 您能否用ruby-“ Twitter :: Tweet”中的oop范式解释这意味着什么? does it mean that the tweet object is part of the Twitter api and Tweet class?
这是否意味着tweet对象是Twitter api和Tweet类的一部分?
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. false
参数在这里很重要,它将删除父类的所有方法。 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. 对于第二个问题:
Twitter
是所有与gem相关的类的主要模块,它用于不将Twitter仅与Twitter相关的类弄乱全局名称空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.