Ruby/Statement/unless

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

Syntax of the full unless statement:

unless Boolean [then | :]
  code
]
[else
  code ]
end



To get the opposite effect of if you can use the word unless:

age = 24
puts "You"re NOT a teenager" unless age > 12 && age < 20



unless can work in exactly the same way because unless is just the opposite of if:

age = 10
unless age >= 18
  puts "You"re too young to use this system"
  puts "So we"re going to exit your program now"
  exit
end



unless statement simply operates in the logically reverse sense from the if statement

temperature = 76
 
unless temperature < 65 || temperature > 85
  puts "Picnic time!"
else
  puts "Sorry, no picnic today."
end