Forum: Ruby-core [ruby-trunk - Feature #7883][Open] Add Regex#to_proc

Posted by Robert Klemme (robert_k78)
on 2013-02-19 08:59
(Received via mailing list)
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.
Posted by Nobuyoshi Nakada (nobu)
on 2013-02-19 11:24
(Received via mailing list)
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.
Posted by charliesome (Charlie Somerville) (Guest)
on 2013-02-19 11:42
(Received via mailing list)
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.
Posted by Alex Young (regularfry)
on 2013-02-19 11:45
(Received via mailing list)
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
Posted by Robert Klemme (robert_k78)
on 2013-02-19 12:48
(Received via mailing list)
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.
Posted by Magnus Holm (judofyr)
on 2013-02-19 15:03
(Received via mailing list)
> 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…)
Posted by prijutme4ty (Ilya Vorontsov) (Guest)
on 2013-02-19 15:07
(Received via mailing list)
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.
Posted by Lee Jarvis (hiroshi)
on 2013-02-19 18:02
(Received via mailing list)
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.
Posted by Gosha A. (gosha_a)
on 2013-02-21 14:37
(Received via mailing list)
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.
Posted by jjyr (jy j) (Guest)
on 2013-02-21 15:23
(Received via mailing list)
Issue #7883 has been updated by jjyr (jy j).


judofyr (Magnus Holm) wrote:
>  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.
Posted by ko1 (Koichi Sasada) (Guest)
on 2013-02-22 01:05
(Received via mailing list)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.