Feature #7883

Add Regex#to_proc

Added by Robert Klemme about 1 year ago. Updated about 1 year ago.

[ruby-core:52514]
Status:Open
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:core
Target version:next minor

Description

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.

History

#1 Updated by Nobuyoshi Nakada about 1 year ago

You can use /\Ab/.method(:=~).

#2 Updated by Charlie Somerville about 1 year ago

You can use /\Ab/.method(:=~).

This is more typing than just using '{ |x| x =~ /\Ab/ }'.

I like this idea, it's something I have to do quite often.

I've also wanted a 'grepv' method before, but reject with Regexp#toproc would suit perfectly.

#3 Updated by Alex Young about 1 year ago

On 19/02/2013 07:58, rklemme (Robert Klemme) wrote:

Issue #7883 has been reported by rklemme (Robert Klemme).

Lovely. Definite +1 from me (for whatever that's worth).

--
Alex


Feature #7883: Add Regex#to_proc
https://bugs.ruby-lang.org/issues/7883

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.

#4 Updated by Robert Klemme about 1 year ago

nobu (Nobuyoshi Nakada) wrote:

You can use /\Ab/.method(:=~).

Yes, but that's not the point. In that case I'd prefer the real block as Charlie suggested.

#5 Updated by Ilya Vorontsov about 1 year ago

Cool idea. +1

#6 Updated by Magnus Holm about 1 year ago

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…)

#7 Updated by Lee Jarvis about 1 year ago

I like this, and I especially like the modified version from judofyr. You could also do ["abc", 3, "foo", etc].find(&Integer) which is neat, but maybe that's a push in the wrong direction, I'm not sure.

#8 Updated by Gosha Arinich about 1 year ago

Nice one, +1 on it as well.

#9 Updated by jy j about 1 year ago

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!

#10 Updated by Koichi Sasada about 1 year ago

  • Assignee set to Yukihiro Matsumoto
  • Target version changed from 1.9.3 to next minor

Also available in: Atom PDF