Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

array contain only one and specific string object

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

well, then that’s simply

arr2 == ['video']

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

arr2.length == 1 && arr2.first == 'video'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading