What is the unless in Ruby command?

1

Can anyone explain how this unless command works in Ruby?

ruby_is_ugly = false
puts "Ruby não é feio!" unless ruby_is_ugly

It has output

"Ruby não é feio!"
    
asked by anonymous 05.07.2017 / 15:55

2 answers

3

In a nutshell it is an if on the contrary, where it performs the content within the conditional if the condition is not true .

Example:

#!/usr/bin/ruby

x = 1 
unless x == 2
   puts "x é diferente de 2"
 else
   puts "x é igual a 2"
end

Source:

05.07.2017 / 16:09
1

unless is equivalent to using if not, unless and if not (if! condition) is a personal preference. I personally use unless when you do not need an else .

    
20.07.2017 / 00:59