Feature #4086

Feature request: add String#contain? and {Enumerable,Array}#contain? methods

Added by Roger Pack over 3 years ago. Updated almost 2 years ago.

[ruby-core:33335]
Status:Rejected
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:-

Description

=begin
Hello all.

I debated this suggestion previously on ruby talk ( http://www.ruby-forum.com/topic/217473 ) and nobody seemed to have negative feedback, so here goes...

Currently Strings and Enumerables have a contain? method for searching for if one includes another.

I tend to "reach for" an include? method for the same, since it makes more sense in my head. For instance "is this string included in that one? Does this array include this member?"

Since ruby is typically not averse to several ways to do the same thing (example: Array#{detect,find}, Array#{collect,map} etc.) I propose that
new methods be added of String#contain? and {Enumerable,Array}#contain?

I'd be happy to code up a patch if it had a chance of being accepted.
Feedback welcome.
Cheers!
-roger
=end

add_contain_method.diff Magnifier (3.12 KB) Roger Pack, 06/02/2011 03:55 AM

History

#1 Updated by Benoit Daloze over 3 years ago

=begin
On 24 November 2010 17:47, Roger Pack redmine@ruby-lang.org wrote:

Feature #4086: Feature request: add String#contain? and {Enumerable,Array}#contain? methods

Currently Strings and Enumerables have a contain? method for searching for if one includes another.

You probably meant "have an include? method"

I tend to "reach for" an include? method for the same, since it makes more sense in my head.  For instance "is this string included in that one? Does this array include this member?"

There is already #include?, I guess you meant #contain?

Your sentence I just quoted feels right enough to me to not want anything else:
"is this string included in that one? Does this array include this member?"

Especially about Arrays, I prefer #include?

if ary.include? element
if ary.contain? element

However, you might be right, #include? is not the best name for string
inclusion in another.

I would expect String#include? to say me if a single character is part
of the String, because the set of characters is likely the only
enumerable part of a String which make sense here.

But, as we all know, characters in Ruby happen to be String of length
1, and then the current #include? behavior is logical.

#contain? might fit better for substring inclusion, but the
'cleverness' of #include? is nicer to me.

Since ruby is typically not averse to several ways to do the same thing (example: Array#{detect,find}, Array#{collect,map} etc.)

I personally disagree to this, such basic methods duplicated seems bad
design for readers. I like TMTOWTDI, but this is just confusing.

Just an opinion,
Benoit Daloze

=end

#2 Updated by Yukihiro Matsumoto over 3 years ago

=begin
Hi,

In message "Re: [Ruby 1.9-Feature#4086][Open] Feature request: add String#contain? and {Enumerable,Array}#contain? methods"
on Thu, 25 Nov 2010 01:47:28 +0900, Roger Pack redmine@ruby-lang.org writes:
|
|Feature #4086: Feature request: add String#contain? and {Enumerable,Array}#contain? methods
|http://redmine.ruby-lang.org/issues/show/4086

Your proposal is somewhat confusing. Could you repost?
Mind, since 1.9, String is no longer Enumerable, when you repost.

                        matz.

=end

#3 Updated by Roger Pack almost 3 years ago

Ok I am updating to clarify this. The gist of the code change would be this (patch attached):

module Enumerable
alias contain? include?
end

class String
alias contain? include?
end

Which would allow for this to work:
[1,2,3].contain? 3

"a long sentence".contain? "sentence"

s.each_line { |line|
if line.contain? 'abc'
...
end
}

Which feels quite natural to me, and is the method name that I typically "reach for" first, only to have to load some other library to get it.

Benoit wrote:

Since ruby is typically not averse to several ways to do the same thing (example: Array#{detect,find}, Array#{collect,map} etc.)

I personally disagree to this, such basic methods duplicated seems bad
design for readers. I like TMTOWTDI, but this is just confusing.

In this case I feel like it would be ok, though I am traditionally not a big fan of method duplication.

I also feel like it makes more sense to do both String and Enumerable at the same time since they have an "include?" method, so to keep it the same there.

Any feedback?
Thanks!
-roger-

#4 Updated by Roger Pack almost 3 years ago

No negative feedback so I created a patch for it. It would be great to have it reviewed before next feature freeze, whoever may do that.

Thanks!
-roger-

#5 Updated by Shota Fukumori almost 3 years ago

  • Assignee set to Yukihiro Matsumoto

#6 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#7 Updated by Yukihiro Matsumoto about 2 years ago

  • Status changed from Assigned to Rejected

The proposal is ambiguous. Only valid point I see is String#contain? might be more intuitive than String#include?, but I don't see the need for method addition.

Matz.

#8 Updated by Roger Pack almost 2 years ago

The proposal is ambiguous. Only valid point I see is String#contain? might be more intuitive than String#include?, but I don't see the need for method addition.

Yes because of being more intuitive, at least for this user.

Also available in: Atom PDF