Issue #6365 has been updated by jhilden (Jakob Hilden).


(First, sorry for my late reply, I somehow didn't receive notifications that my ticket was updated.)

Thank you very much for thinking about my proposal.  I agree that it is very subjective and I probably also agree that str.include?([str1, str2]) is probably not a very good solution.  However, I still think it would be a big code readability improvement to allow this direction of checking whether a string is included in a collection.

Let me describe an abstrac use case:
Many times I have an Array defined somewher of e.g. valid values, currencies, locales.  Then I get a string from somewhere (maybe user input) and I want to check whether that string is within the valid values.  Naturally I ask "Is this thing I just received included in the collection of all the valid things?".  And it feels very unnatural having to ask instead "Does the collection of values include the thing I just got?".

I would be very glad if you would reconsider my proposal and I am convinced that it would improve the Syntax of the language.


----------------------------------------
Feature #6365: [Feature request] Better readable syntax to check if String is included in Array
https://bugs.ruby-lang.org/issues/6365#change-26605

Author: jhilden (Jakob Hilden)
Status: Rejected
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: 
Target version: 


=begin
I find myself constantly wanting to check whether one string is included within an array of strings.
It is certainly possible (and also fast) to do that in Ruby with something like this: (({["foo", "bar"].include?("foo")}))
But I don't think it reads very nice :(
Because what I actually want to test is, whether my string is included in the array and NOT the other way around.

What do you think about something like the following two solutions?

  class String
    # create a new method
    def included_in?(array)
      array.include?(self)
    end

    # -- OR --

    # change the current String#include? method
    def include?(parameter)
      if parameter.is_a? Array
        parameter.include?(self)
      else
        super
      end
    end
  end

I know it's just a minor code vanity issue, but since it's one of Ruby's main features, I wanted to bring it up.

The (pseudo) code can also be found here:  ((<"https://gist.github.com/1181246"|URL:https://gist.github.com/1181246>))
=end



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