In Ruby, How to Find Out If a String Is Not in an Array

In Ruby, how do I find out if a string is not in an array?

do_this unless  ["www", "blog", "foo", "bar"].include?(current_subdomain)

or

do_this if not ["www", "blog", "foo", "bar"].include?(current_subdomain)

I'm using the Array#include? method.

However using unless is a fairly big ruby idiom.

How to check if a value exists in an array in Ruby

You're looking for include?:

>> ['Cat', 'Dog', 'Bird'].include? 'Dog'
=> true

How to check if a string includes any value of an array?

To check when the whole word is included case sensitive:

(str.split & arr).any?
#⇒ true

Case insensitive:

[str.split, arr].map { |a| a.map(&:downcase) }.reduce(&:&).any?
#⇒ true

To check if it includes any of arr anyhow:

arr.any?(&str.method(:include?))
#⇒ true

Determining if a string contains any of the value that is present in an array in ruby

Here's how I would write it:

my_string = "testing.facebook.com"
array = ["google.com","facebook.com","github.com"]

array.any? { |item| my_string.include?(item) }

This uses Enumerable#any?.

Enumerable is a module which is included in the Array class, amongst others.

How to check if my string matches an array element in Ruby

Because STATUSES is an array, find the first element that matches done and compare:

done_status = STATUSES.detect { |status| status == 'done' } 
if str == done_status
# do something
end

How to check in ruby if an string contains any of an array of strings

There you go using #any?.

a = ['key','words','to', 'check']
b = "this is a long string"
a.any? { |s| b.include? s }

Or something like using ::union. But as per the need, you might need to change the regex, to some extent it can be done. If not, then I will go with above one.

a = ['key','words','to', 'check'] 
Regexp.union a
# => /key|words|to|check/
b = "this is a long string"
Regexp.union(a) === b # => false

How can I tell if my string contains a substring from an array?

Given:

arr = ["a", "b", "x"]
str = "DoDox"

EDIT: As the other comments have pointed out, this is actually the fastest way, as it breaks the evaluation when a single true statement is found:

arr.any?{|substr| str.include?(substr)}

The foldl way: I have used this example to illustrate the power of folding expresions. This solution might be somewhat elegant as an illustrative example for foldings, but it is not sufficiently performant.

arr.map{|substr| str.include?(substr)}.inject{|x,y| x || y}

The map-function maps all possible substrings the original string str can have. inject iterates over the returning array and compares its constituents, using a logical or. If any substring in the array 'arr' is encountered, the result will yield true in the result of map. With the or-relation we return a true value, if any value of the result of map was true. This is slower than any?, because every single value in the array gets evaluated even if a true is encountered (wich would always yield a true at the end of inject).

["a", "b", "x"] -------> [false, false, true] ----------> true
map() inject()

Ruby - test for array

You probably want to use kind_of().

>> s = "something"
=> "something"
>> s.kind_of?(Array)
=> false
>> s = ["something", "else"]
=> ["something", "else"]
>> s.kind_of?(Array)
=> true


Related Topics



Leave a reply



Submit