ITeye博客

ruby 的 Enumerable 方法

夜鸣猪 · 2012-02-23 03:28 · Ruby 1.9 · 编程语言 · Markdown

grep 是===来判断是否存在符合条件的sub enumerable


c = IO.constants
c.grep(/SEEK/)         #=> [:seek_set, :seek_cur, :seek_end]
res = c.grep(/SEEK/) {|v| IO.const_get(v) }
res                    #=> [0, 1, 2]

用来差方法很不错


array.public_methods.grep(/^each_(.*)$/){ puts $1}
#index
#with_index
#entry
#slice
#cons
#with_object

评论(1)

  1. #1 楼 · fireDragonpzy · 2012-02-23 15:41

    Array.instance_methods

评论功能维护中,当前仅展示历史评论。