当前位置:  开发笔记 > 编程语言 > 正文

Ruby相当于Python的"dir"?

如何解决《Ruby相当于Python的"dir"?》经验,为你挑选了2个好方法。

在Python中,我们可以"dir"一个模块,如下所示:

>>> import re
>>> dir(re)

它列出了模块中的所有功能.在Ruby中有类似的方法吗?



1> Jonas Elfstr..:

据我所知不完全,但你得到的地方

object.methods.sort



2> Commander Ke..:

我喜欢在我的.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"]

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有