ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc
@ 2013-02-19  7:58 rklemme (Robert Klemme)
  2013-02-19 10:24 ` [ruby-core:52516] [ruby-trunk - Feature #7883] " nobu (Nobuyoshi Nakada)
                   ` (13 more replies)
  0 siblings, 14 replies; 15+ messages in thread
From: rklemme (Robert Klemme) @ 2013-02-19  7:58 UTC (permalink / raw
  To: ruby-core


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

----------------------------------------
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.


-- 
http://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52516] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
@ 2013-02-19 10:24 ` nobu (Nobuyoshi Nakada)
  2013-02-19 10:41 ` [ruby-core:52517] " charliesome (Charlie Somerville)
                   ` (12 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2013-02-19 10:24 UTC (permalink / raw
  To: ruby-core


Issue #7883 has been updated by nobu (Nobuyoshi Nakada).


You can use /\Ab/.method(:=~).
----------------------------------------
Feature #7883: Add Regex#to_proc
https://bugs.ruby-lang.org/issues/7883#change-36601

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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52517] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
  2013-02-19 10:24 ` [ruby-core:52516] [ruby-trunk - Feature #7883] " nobu (Nobuyoshi Nakada)
@ 2013-02-19 10:41 ` charliesome (Charlie Somerville)
  2013-02-19 10:44 ` [ruby-core:52518] Re: [ruby-trunk - Feature #7883][Open] " Alex Young
                   ` (11 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: charliesome (Charlie Somerville) @ 2013-02-19 10:41 UTC (permalink / raw
  To: ruby-core


Issue #7883 has been updated by charliesome (Charlie Somerville).


> 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.
----------------------------------------
Feature #7883: Add Regex#to_proc
https://bugs.ruby-lang.org/issues/7883#change-36602

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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52518] Re: [ruby-trunk - Feature #7883][Open] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
  2013-02-19 10:24 ` [ruby-core:52516] [ruby-trunk - Feature #7883] " nobu (Nobuyoshi Nakada)
  2013-02-19 10:41 ` [ruby-core:52517] " charliesome (Charlie Somerville)
@ 2013-02-19 10:44 ` Alex Young
  2013-02-19 11:47 ` [ruby-core:52520] [ruby-trunk - Feature #7883] " rklemme (Robert Klemme)
                   ` (10 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Alex Young @ 2013-02-19 10:44 UTC (permalink / raw
  To: ruby-core

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.
>
>

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52520] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (2 preceding siblings ...)
  2013-02-19 10:44 ` [ruby-core:52518] Re: [ruby-trunk - Feature #7883][Open] " Alex Young
@ 2013-02-19 11:47 ` rklemme (Robert Klemme)
  2013-02-19 14:03 ` [ruby-core:52527] Re: [ruby-trunk - Feature #7883][Open] " Magnus Holm
                   ` (9 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: rklemme (Robert Klemme) @ 2013-02-19 11:47 UTC (permalink / raw
  To: ruby-core


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


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.
----------------------------------------
Feature #7883: Add Regex#to_proc
https://bugs.ruby-lang.org/issues/7883#change-36606

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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52527] Re: [ruby-trunk - Feature #7883][Open] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (3 preceding siblings ...)
  2013-02-19 11:47 ` [ruby-core:52520] [ruby-trunk - Feature #7883] " rklemme (Robert Klemme)
@ 2013-02-19 14:03 ` Magnus Holm
  2013-02-19 14:06 ` [ruby-core:52528] [ruby-trunk - Feature #7883] " prijutme4ty (Ilya Vorontsov)
                   ` (8 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: Magnus Holm @ 2013-02-19 14:03 UTC (permalink / raw
  To: ruby-core

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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52528] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (4 preceding siblings ...)
  2013-02-19 14:03 ` [ruby-core:52527] Re: [ruby-trunk - Feature #7883][Open] " Magnus Holm
@ 2013-02-19 14:06 ` prijutme4ty (Ilya Vorontsov)
  2013-02-19 16:59 ` [ruby-core:52535] " injekt (Lee Jarvis)
                   ` (7 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: prijutme4ty (Ilya Vorontsov) @ 2013-02-19 14:06 UTC (permalink / raw
  To: ruby-core


Issue #7883 has been updated by prijutme4ty (Ilya Vorontsov).


Cool idea. +1
----------------------------------------
Feature #7883: Add Regex#to_proc
https://bugs.ruby-lang.org/issues/7883#change-36617

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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52535] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (5 preceding siblings ...)
  2013-02-19 14:06 ` [ruby-core:52528] [ruby-trunk - Feature #7883] " prijutme4ty (Ilya Vorontsov)
@ 2013-02-19 16:59 ` injekt (Lee Jarvis)
  2013-02-21 13:36 ` [ruby-core:52624] " goshakkk (Gosha Arinich)
                   ` (6 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: injekt (Lee Jarvis) @ 2013-02-19 16:59 UTC (permalink / raw
  To: ruby-core


Issue #7883 has been updated by injekt (Lee Jarvis).


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.
----------------------------------------
Feature #7883: Add Regex#to_proc
https://bugs.ruby-lang.org/issues/7883#change-36624

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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52624] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (6 preceding siblings ...)
  2013-02-19 16:59 ` [ruby-core:52535] " injekt (Lee Jarvis)
@ 2013-02-21 13:36 ` goshakkk (Gosha Arinich)
  2013-02-21 14:23 ` [ruby-core:52625] " jjyr (jy j)
                   ` (5 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: goshakkk (Gosha Arinich) @ 2013-02-21 13:36 UTC (permalink / raw
  To: ruby-core


Issue #7883 has been updated by goshakkk (Gosha Arinich).


Nice one, +1 on it as well.
----------------------------------------
Feature #7883: Add Regex#to_proc
https://bugs.ruby-lang.org/issues/7883#change-36710

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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52625] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (7 preceding siblings ...)
  2013-02-21 13:36 ` [ruby-core:52624] " goshakkk (Gosha Arinich)
@ 2013-02-21 14:23 ` jjyr (jy j)
  2013-02-22  0:04 ` [ruby-core:52647] " ko1 (Koichi Sasada)
                   ` (4 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: jjyr (jy j) @ 2013-02-21 14:23 UTC (permalink / raw
  To: ruby-core


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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:52647] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (8 preceding siblings ...)
  2013-02-21 14:23 ` [ruby-core:52625] " jjyr (jy j)
@ 2013-02-22  0:04 ` ko1 (Koichi Sasada)
  2014-12-23  0:01 ` [ruby-core:67047] " shortcutter
                   ` (3 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: ko1 (Koichi Sasada) @ 2013-02-22  0:04 UTC (permalink / raw
  To: ruby-core


Issue #7883 has been updated by ko1 (Koichi Sasada).

Assignee set to matz (Yukihiro Matsumoto)
Target version changed from 1.9.3 to next minor


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

Author: rklemme (Robert Klemme)
Status: Open
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: next minor


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/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:67047] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (9 preceding siblings ...)
  2013-02-22  0:04 ` [ruby-core:52647] " ko1 (Koichi Sasada)
@ 2014-12-23  0:01 ` shortcutter
  2014-12-23  3:09 ` [ruby-core:67057] " nobu
                   ` (2 subsequent siblings)
  13 siblings, 0 replies; 15+ messages in thread
From: shortcutter @ 2014-12-23  0:01 UTC (permalink / raw
  To: ruby-core

Issue #7883 has been updated by Robert Klemme.


Was any of the two suggested forms ever realized?

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

* Author: Robert Klemme
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: core
* Target version: next minor
----------------------------------------
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.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:67057] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (10 preceding siblings ...)
  2014-12-23  0:01 ` [ruby-core:67047] " shortcutter
@ 2014-12-23  3:09 ` nobu
  2014-12-23 13:15 ` [ruby-core:67067] " shortcutter
  2014-12-24  1:17 ` [ruby-core:67084] " sawadatsuyoshi
  13 siblings, 0 replies; 15+ messages in thread
From: nobu @ 2014-12-23  3:09 UTC (permalink / raw
  To: ruby-core

Issue #7883 has been updated by Nobuyoshi Nakada.

Description updated

Nothing, and line 10 is possible with `grep`

~~~ruby
%w{foo bar baz}.grep(/\Ab/) {|s|break s}
~~~

Anyway, a patch.

~~~diff
diff --git i/re.c w/re.c
index 1a0f0b1..d345f17 100644
--- i/re.c
+++ w/re.c
@@ -801,6 +801,19 @@ rb_reg_named_captures(VALUE re)
     return hash;
 }
 
+static VALUE
+rb_reg_to_proc(VALUE re)
+{
+    const ID id = 1;
+    VALUE proc = rb_attr_get(re, id);
+    if (NIL_P(proc)) {
+	proc = rb_obj_method(re, ID2SYM(rb_intern("=~")));
+	proc = rb_funcall(proc, rb_intern("to_proc"), 0, 0);
+	rb_ivar_set(re, id, proc);
+    }
+    return proc;
+}
+
 static int
 onig_new_with_source(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
 	  OnigOptionType option, OnigEncoding enc, const OnigSyntaxType* syntax,
@@ -3660,6 +3673,7 @@ Init_Regexp(void)
     rb_define_method(rb_cRegexp, "fixed_encoding?", rb_reg_fixed_encoding_p, 0);
     rb_define_method(rb_cRegexp, "names", rb_reg_names, 0);
     rb_define_method(rb_cRegexp, "named_captures", rb_reg_named_captures, 0);
+    rb_define_method(rb_cRegexp, "to_proc", rb_reg_to_proc, 0);
 
     /* see Regexp.options and Regexp.new */
     rb_define_const(rb_cRegexp, "IGNORECASE", INT2FIX(ONIG_OPTION_IGNORECASE));
~~~


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

* Author: Robert Klemme
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: core
* Target version: next minor
----------------------------------------
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` 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

~~~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` AFAIK.

I see low risk of breaking something.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [ruby-core:67067] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (11 preceding siblings ...)
  2014-12-23  3:09 ` [ruby-core:67057] " nobu
@ 2014-12-23 13:15 ` shortcutter
  2014-12-24  1:17 ` [ruby-core:67084] " sawadatsuyoshi
  13 siblings, 0 replies; 15+ messages in thread
From: shortcutter @ 2014-12-23 13:15 UTC (permalink / raw
  To: ruby-core

Issue #7883 has been updated by Robert Klemme.


Nobuyoshi Nakada wrote:
> Nothing, and line 10 is possible with `grep`
> 
> ~~~ruby
> %w{foo bar baz}.grep(/\Ab/) {|s|break s}
> ~~~

Yes, but a bit lengthy.

> Anyway, a patch.

Cool!  Thanks a lot!

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

* Author: Robert Klemme
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: core
* Target version: next minor
----------------------------------------
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` 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

~~~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` AFAIK.

I see low risk of breaking something.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [ruby-core:67084] [ruby-trunk - Feature #7883] Add Regex#to_proc
  2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
                   ` (12 preceding siblings ...)
  2014-12-23 13:15 ` [ruby-core:67067] " shortcutter
@ 2014-12-24  1:17 ` sawadatsuyoshi
  13 siblings, 0 replies; 15+ messages in thread
From: sawadatsuyoshi @ 2014-12-24  1:17 UTC (permalink / raw
  To: ruby-core

Issue #7883 has been updated by Tsuyoshi Sawada.


If the proposed patch to issue #9602 gets accepted, then that will covert this issue in a  more general way.

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

* Author: Robert Klemme
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
* Category: core
* Target version: next minor
----------------------------------------
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` 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

~~~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` AFAIK.

I see low risk of breaking something.



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2014-12-24  1:22 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-19  7:58 [ruby-core:52514] [ruby-trunk - Feature #7883][Open] Add Regex#to_proc rklemme (Robert Klemme)
2013-02-19 10:24 ` [ruby-core:52516] [ruby-trunk - Feature #7883] " nobu (Nobuyoshi Nakada)
2013-02-19 10:41 ` [ruby-core:52517] " charliesome (Charlie Somerville)
2013-02-19 10:44 ` [ruby-core:52518] Re: [ruby-trunk - Feature #7883][Open] " Alex Young
2013-02-19 11:47 ` [ruby-core:52520] [ruby-trunk - Feature #7883] " rklemme (Robert Klemme)
2013-02-19 14:03 ` [ruby-core:52527] Re: [ruby-trunk - Feature #7883][Open] " Magnus Holm
2013-02-19 14:06 ` [ruby-core:52528] [ruby-trunk - Feature #7883] " prijutme4ty (Ilya Vorontsov)
2013-02-19 16:59 ` [ruby-core:52535] " injekt (Lee Jarvis)
2013-02-21 13:36 ` [ruby-core:52624] " goshakkk (Gosha Arinich)
2013-02-21 14:23 ` [ruby-core:52625] " jjyr (jy j)
2013-02-22  0:04 ` [ruby-core:52647] " ko1 (Koichi Sasada)
2014-12-23  0:01 ` [ruby-core:67047] " shortcutter
2014-12-23  3:09 ` [ruby-core:67057] " nobu
2014-12-23 13:15 ` [ruby-core:67067] " shortcutter
2014-12-24  1:17 ` [ruby-core:67084] " sawadatsuyoshi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).