在Python中,我们可以"dir"一个模块,如下所示:
>>> import re >>> dir(re)
它列出了模块中的所有功能.在Ruby中有类似的方法吗?
据我所知不完全,但你得到的地方
object.methods.sort
我喜欢在我的.irbrc中有这个:
class Object def local_methods (methods - Object.instance_methods).sort end end
所以当我在irb时:
>> Time.now.local_methods => ["+", "-", "<", "<=", "<=>", ">", ">=", "_dump", "asctime", "between?", "ctime", "day", "dst?", "getgm", "getlocal", "getutc", "gmt?", "gmt_offset", "gmtime", "gmtoff", "hour", "isdst", "localtime", "mday", "min", "mon", "month", "sec", "strftime", "succ", "to_f", "to_i", "tv_sec", "tv_usec", "usec", "utc", "utc?", "utc_offset", "wday", "yday", "year", "zone"]
甚至可爱 - 用grep:
>> Time.now.local_methods.grep /str/ => ["strftime"]