Project

General

Profile

Actions

Feature #6365

closed

[Feature request] Better readable syntax to check if String is included in Array

Added by jhilden (Jakob Hilden) over 12 years ago. Updated over 12 years ago.

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

Description

=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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0