rubycoding-style

Ruby: unless vs if not


I find myself preferring if not rather than unless. Is there a proper way to write that sort of condition? How do people generally feel about unless?


Solution

  • I hope this helps you: https://github.com/rubocop-hq/ruby-style-guide#if-vs-unless

    Prefer unless over if for negative conditions (or control flow ||).

    # bad
    do_something if !some_condition
    
    # bad
    do_something if not some_condition
    
    # good
    do_something unless some_condition
    

    I personally agree with what's written there choosing unless something over if !something for modifiers and simple ones and when there's an else prefer if.