Home > database >  array contain only one and specific string object
array contain only one and specific string object

Time:09-29

I have a usecase where i wanted to check if array contains only specific string element.

arr = ['video','audio', 'jpeg']

arr2 = ['video']

from above two array I wanted to check if arr2 contains only one element and it has to be only 'video'.

Does Ruby has any array method to do so or we only write our own custom logic to have this usecase. I am using arr2.all?('video') method which is returning true but it does not fulfilling my requirement.

CodePudding user response:

well, then that's simply

arr2 == ['video']

otherwise, if you don't want to use the literal directly

arr2.length == 1 && arr2.first == 'video'
  • Related