Ruby/Statement/If

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

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

Содержание

An if statement with an else clause looks like in general (the else clause is optional)

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



Conditional Statements

value = 0
if value.zero? then
  puts "value is zero. Did you guess that one?"
end



if and unless also supply the else condition

# used to delimit lines of code that you want to be executed if the main expression is false:
age = 10
if age < 18
  puts "You"re too young to use this system"
else
  puts "You can use this system"
end



if statement contains several elsif statements

lang = :es
if lang == :en
  print "dog"
elsif lang == :es
  print "perro"
elsif lang == :fr
  print "chien"
elsif lang == :de
  print "Hund"
else
  puts "No language set; default = "dog"."
end
# "perro" is assigned to dog



lay out an if statement is by replacing the then with a colon (:)

x = 256
if x == 256: puts "x equals 256" end



nest if logic

age = 19
if age < 21
  puts "You can"t drink in most of the United States"
  if age >= 18
    puts "But you can in the United Kingdom!"
  end
end



put any number of lines of code in between the if statement and the end line:

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



Syntax of using elsif Clauses in if Statements

if (condition1)
  code to be executed if condition1 is true
elsif (condition2)
  code to be executed if condition2 is true
else
  code to be executed if condition1 and condition2 are not true



The if Statement with constant

if 1 == 1 then
  print "True!"
end



The simple form of an if statement looks like this

if Boolean [then | :]
  code
end



Use an elsif Clause

temperature = 76
 
if temperature > 85
  puts "Too hot!"
elsif temperature < 65
  puts "Too cold!"
else
  puts "Picnic time!"
end



Use if statement to check the user input

print "(t)imes or (p)lus: " 
times = gets 
print "number: " 
number = Integer(gets) 
if times =~ /^t/ 
puts((1..10).collect {|n| n*number }.join(", ")) 
else 
puts((1..10).collect {|n| n+number }.join(", ")) 
end



Use the if Statement

temperature = 76
 
if temperature > 65 && temperature < 85
      puts "Picnic time!"
end



use the ! logical negation operator

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



use the multiline if/else option

age = 10
if age < 18
  type = "child"
else
  type = "adult"
end
puts "You are a " + type



Using else Clauses in if Statements

temperature = 43
if temperature > 65 && temperature < 85
  puts "Picnic time!"
else
  puts "Sorry, no picnic today."
end



we dropped then from the if statement.

# In addition, you don"t have to use end if you write this code all on one line, like so:
x = 256
if x == 256 then puts "x equals 256" end



write elsif statement a little tighter by using colons after the symbols:

if lang == :en: print "dog"
 elsif lang == :es: print "perro"
 elsif lang == :fr: print "chien"
 elsif lang == :de: print "Hund"
 else puts "No language set; default = "dog"."
end