I get arrays of different urls from a database column. Both are used to apply a whitelist on a url (ex. www.example.com/test/page.html). One is for the url beginning, one for the end.
whitelist_start = self.domain.whitelists.map(&:url_start)
whitelist_end = self.domain.whitelists.map(&:url_end)
Before I insert my url in the database, I want to check if save_url is true. After that, the filtered url will be inserted. The url can ether start (ex. www.example.com/test) or end with an url (ex. page.html). But the whole whitelist can also be empty at the same time. One of these three things.
save_url = url.starts_with?(*whitelist_start) || url.ends_with?(*whitelist_end) || whitelist_start.empty? || whitelist_end.empty?
I'm using the * before the array name in starts_with? and ends_with? because I want to insert all array values there. However, if one array is empty (which is totally fine) I get this error and can't proceed.
no implicit conversion of nil into String
Is there a way to solve this?
It's occurring because you have a nil position on your arrays, try this
whitelist_start = self.domain.whitelists.map(&:url_start).compact
whitelist_end = self.domain.whitelists.map(&:url_end).compact
I will show you, your array is not empty
2.3.1 :017 > test = []
=> []
2.3.1 :019 > "string".starts_with?(*test)
=> false
And now, your nil
array position
2.3.1 :020 > test = [nil]
=> [nil]
2.3.1 :021 > "string".starts_with?(*test)
TypeError: no implicit conversion of nil into String
In my tests, this error occur only when nil
is in the first position
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With