Ruby conditions + multiple ORs

Code refactoring for multiple OR conditions, Here is an example of weird looking conditional statement

if name.blank? || surname.blank? || nickname.blank? || aka.blank?
  ...
end
Lets try and refactor this
Option 1 
if [name, surname, nickname, aka].any? { |field| field.blank? }
  ...
end
Option 2
if [name, surname, nickname, aka].any?(&:blank?)
  ...
end

Comments

Popular posts from this blog

Inserting and Moving elements inside Ruby Array

Remove duplicates from a JavaScript Array

Sending Binary Data over the Network with Json