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!"
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!"
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:
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 .