Project

General

Profile

Actions

Feature #4086

closed

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

Added by rogerdpack (Roger Pack) over 13 years ago. Updated over 11 years ago.

Status:
Rejected
Target version:
-
[ruby-core:33335]

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


Files

add_contain_method.diff (3.12 KB) add_contain_method.diff rogerdpack (Roger Pack), 06/02/2011 03:55 AM
Actions #1

Updated by Eregon (Benoit Daloze) over 13 years ago

=begin
On 24 November 2010 17:47, Roger Pack 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

Actions #2

Updated by matz (Yukihiro Matsumoto) over 13 years ago

=begin
Hi,

In message "Re: [ruby-core:33335] [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 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

Updated by rogerdpack (Roger Pack) almost 13 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-

Updated by rogerdpack (Roger Pack) almost 13 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-

Updated by sorah (Sorah Fukumori) almost 13 years ago

  • Assignee set to matz (Yukihiro Matsumoto)
Actions #6

Updated by shyouhei (Shyouhei Urabe) about 12 years ago

  • Status changed from Open to Assigned

Updated by matz (Yukihiro Matsumoto) almost 12 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.

Updated by rogerdpack (Roger Pack) over 11 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0