From: "jjyr (jy j)" Date: 2013-02-21T23:23:23+09:00 Subject: [ruby-core:52625] [ruby-trunk - Feature #7883] Add Regex#to_proc Issue #7883 has been updated by jjyr (jy j). judofyr (Magnus Holm) wrote: > > class Regexp > > def to_proc; lambda {|s| self =~ s} end > > end > > A more general case: > > class Object > def to_proc > lambda { |other| self === other } > end > end > > (Not that I think this is going to be accepted���) cool! ---------------------------------------- Feature #7883: Add Regex#to_proc https://bugs.ruby-lang.org/issues/7883#change-36712 Author: rklemme (Robert Klemme) Status: Open Priority: Normal Assignee: Category: core Target version: 1.9.3 Just a small addition to the standard library: class Regexp def to_proc; lambda {|s| self =~ s} end end With that one can use a Regex everywhere a Proc is used as filtering criteria saving a bit of typing. While we have Enumerable#grep already there may be other cases where you want to do something like irb(main):008:0> %w{foo bar baz}.select &/\Ab/ => ["bar", "baz"] irb(main):009:0> %w{foo bar baz}.reject &/\Ab/ => ["foo"] irb(main):010:0> %w{foo bar baz}.find &/\Ab/ => "bar" Note: line 9 and 10 are not possible with Enumerable#grep AFAIK. I see low risk of breaking something. -- http://bugs.ruby-lang.org/