Project

General

Profile

Feature #7883

Updated by nobu (Nobuyoshi Nakada) over 5 years ago

Just a small addition to the standard library: 

 ~~~ruby 
 class Regexp 
   def to_proc; lambda {|s| self =~ s} end 
 end 
 ~~~ 

 With that one can use a `Regexp` Regex everywhere a `Proc` Proc is used as filtering criteria saving a bit of typing.    While we have `Enumerable#grep` Enumerable#grep already there may be other cases where you want to do something like 

 ~~~ruby 
 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` Enumerable#grep AFAIK. 

 I see low risk of breaking something.

Back