Currently I have this code:
if !(allowed_params & password_protected_params).empty?
Which means "if anything in allowed_params is also in password_protected_params". This code works, but I find it a bit obfuscated and not friendly to the next developer that looks at this code.
Is there another, more readable way to check if anything in one array exists within another array?
EDIT
The block in question is this:
if !(allowed_params & password_protected_params).empty?
result = @user.update_with_password(allowed_params)
else
#only update trivial attributes
result = @user.update_without_password(allowed_params)
end
In the end I just added a variable to make it more readable (but still open to better suggestions):
needs_password = !(allowed_params & password_protected_params).empty?
if needs_password
result = @user.update_with_password(allowed_params)
else
#only update trivial attributes
result = @user.update_without_password(allowed_params)
end
if (allowed_params & password_protected_params).any?empty?, I'm mainly wondering if there's a more human (read: non-expert in Ruby) readable way to show "yes, one of the password protected params is in allowed_params". In particular, the[] & []operator drew blanks from fellow JS devs, and a couple of Ruby devs too.!) by substituting a complementary method, hereany?rather thanempty?. Your use of&seems perfectly clear. Sadly, your code being obscure is not the only possible explanation for some Ruby developers being unable to comprehend it.