Feature #8921
openAllow select, reject, etc to accept a regex
Added by kyledecot (Kyle Decot) about 12 years ago. Updated over 5 years ago.
Description
It would be handy if select could accept a regex. For example
%w[foo bar baz].select(/^ba/) # => ["bar", "baz"]
This is currently possible via the slightly longer syntax
%w[foo bar baz].select{|i| i[/^ba/]} # => ["bar", "baz"]
Updated by Hanmac (Hans Mackowiak) about 12 years ago
Actions
#1
[ruby-core:57263]
- it can be done via
class Regexp
def to_proc
proc {|o| self.match(o) }
end
end
- but i think the method you are looking for is grep
%w[foo bar baz].grep /^ba/ # => ["bar", "baz"]
Updated by kyledecot (Kyle Decot) about 12 years ago
Actions
#2
[ruby-core:57264]
=begin
Yes, grep would be a suitable alternative for select w/ a regex in this instance. What about for reject though? I feel that
%w[foo bar baz].reject /^ba/ # ["foo"]
Is more readable than
%w[foo bar baz].grep /^[^ba]/ # ["foo"]
Using reject also makes it so that I don't have to rewrite my regular expression to negate what I'm looking for. My example is pretty trivial but I think that it would quickly get out of hand if trying to grep things "out" instead of "in".
=end
Updated by fuadksd (Fuad Saud) about 12 years ago
Actions
#3
[ruby-core:57265]
Shouldn't select/reject use threequals?
On Sep 18, 2013 12:25 PM, "kyledecot (Kyle Decot)" kyle.decot@icloud.com
wrote:
Issue #8921 has been updated by kyledecot (Kyle Decot).
=begin
Yes, grep would be a suitable alternative forselectw/ a regex in this
instance. What about forrejectthough? I feel that%w[foo bar baz].reject /^ba/ # ["foo"]Is more readable than
%w[foo bar baz].grep /^[^ba]/ # ["foo"]Using
rejectalso makes it so that I don't have to rewrite my regular
expression to negate what I'm looking for. My example is pretty trivial but
I think that it would quickly get out of hand if trying to grep things
"out" instead of "in".
=end¶Feature #8921: Allow select, reject, etc to accept a regex
https://bugs.ruby-lang.org/issues/8921#change-41882Author: kyledecot (Kyle Decot)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:It would be really handy if for instance
selectcould accept a regex.
For example%w[foo bar baz].select /^ba/ # ["bar", "baz"]I know that this is currently possible via the slightly longer syntax
%w[foo bar baz].select{|i| i[/^ba/] } # ["bar", "baz"]
Updated by fuadksd (Fuad Saud) about 12 years ago
Actions
#4
[ruby-core:57266]
I mean, couldn't select, reject and friends take one parameter in case no
block is passed and compare elements using the threequals operator. This
would enable one to do things like:
[1, 56, 12, 7, 39].select 0..20 #=> [1, 12]
%w( foo bar baz ).reject /^ba/ #=> [foo]
On Wednesday, September 18, 2013, Fuad Saud wrote:
Shouldn't select/reject use threequals?
On Sep 18, 2013 12:25 PM, "kyledecot (Kyle Decot)" <kyle.decot@icloud.com<javascript:_e({}, 'cvml', 'kyle.decot@icloud.com');>>
wrote:Issue #8921 has been updated by kyledecot (Kyle Decot).
=begin
Yes, grep would be a suitable alternative forselectw/ a regex in this
instance. What about forrejectthough? I feel that%w[foo bar baz].reject /^ba/ # ["foo"]Is more readable than
%w[foo bar baz].grep /^[^ba]/ # ["foo"]Using
rejectalso makes it so that I don't have to rewrite my regular
expression to negate what I'm looking for. My example is pretty trivial but
I think that it would quickly get out of hand if trying to grep things
"out" instead of "in".
=end¶Feature #8921: Allow select, reject, etc to accept a regex
https://bugs.ruby-lang.org/issues/8921#change-41882Author: kyledecot (Kyle Decot)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:It would be really handy if for instance
selectcould accept a regex.
For example%w[foo bar baz].select /^ba/ # ["bar", "baz"]I know that this is currently possible via the slightly longer syntax
%w[foo bar baz].select{|i| i[/^ba/] } # ["bar", "baz"]
--
Fuad Saud
twitter http://twitter.com/fuadsaud |
linkedinhttp://www.linkedin.com/in/fuadksd|
coderwall http://coderwal.com/fuadsaud | githubhttp://github.com/fuadsaud|
Updated by nobu (Nobuyoshi Nakada) about 12 years ago
Actions
#5
[ruby-core:57267]
- Description updated (diff)
Updated by Anonymous about 12 years ago
Actions
#6
[ruby-core:57268]
On 09/18/2013 09:17 AM, Fuad Saud wrote:
I mean, couldn't select, reject and friends take one parameter in case
no block is passed and compare elements using the threequals operator.
This would enable one to do things like:[1, 56, 12, 7, 39].select 0..20 #=> [1, 12]
%w( foo bar baz ).reject /^ba/ #=> [foo]
And then, because of Proc#===, there is this symmetry:
big = proc {|x| x>10}
=> #Proc:0x007f4770fcd730@(irb):7
[1,2,34].select &pr
=> [34]
[1,2,34].select pr
=> [34] # with proposed change
Updated by Anonymous about 12 years ago
Actions
#7
[ruby-core:57269]
@Hanmac: That's it, thanks!
Updated by nobu (Nobuyoshi Nakada) about 12 years ago
Actions
#8
[ruby-core:57271]
That is grep.
Updated by kyledecot (Kyle Decot) about 12 years ago
Actions
#9
[ruby-core:57272]
Again, I understand that grep can do this but I feel that (especially in my reject example) that the intent is much clearer w/ my proposed addition. What would be the downside to adding something like this to select/reject (or Enumberable in general)?
Updated by fuadksd (Fuad Saud) about 12 years ago
Actions
#10
[ruby-core:57864]
Hey nobu have you thought about this? I think it's a pretty interesting change to be introduced.
Updated by kyledecot (Kyle Decot) over 9 years ago
Actions
#11
[ruby-core:73905]
Has there been any thought on this?
Updated by shyouhei (Shyouhei Urabe) over 9 years ago
Actions
#12
[ruby-core:74102]
Do you still need this? We now have grep_v which resembles your reject example.
Updated by shyouhei (Shyouhei Urabe) over 9 years ago
Actions
#13
- Related to Feature #11049: Enumerable#grep_v (inversed grep) added
Updated by sawa (Tsuyoshi Sawada) over 5 years ago
Actions
#14
- Description updated (diff)