Check if all values from array start with a string

I have an array of links:

urls =
[
  ["http://s3.amazonxaws.com/bb_images/uk/media_images/2/1-veXRiLqrxPbguaxFIcFbPA.png"],
  ["http://s3.amazonaws.com/bb_images/uk/media_images/2/1-veXRiLqrxPbguaxFIcFbPA.png"]
]

How can I return true if all links start with "http://s3.amazonaws.com"?

I tried:

if urls.any? { |url| url.include?('http://s3.amazonaws.com/') }
     #execute code
else
     #execute else code
end

But it does not return true or false in cases.

Upvotes: 2

Views: 687

Answers (2)

djaszczurowski
djaszczurowski

Reputation: 4533

@sawa's code works. However, the following is a more efficient version.

compare_against = "http://s3.amazonaws.com"
urls.all?{|url_array| url_array[0].start_with?(compare_against)}

Upvotes: 4

sawa
sawa

Reputation: 168269

urls.flatten.all?{|s| s.start_with?("http://s3.amazonaws.com")}

Upvotes: 5

Related Questions