Ruby编程语言学习笔记4

对应Ruby编程语言第五章

#Ruby使用换行符、分号、then关键字 对条件表达式(expression)和后续内容(code)进行分割
#
if条件式
=begin
if expression 
  code
end
或者 if expression then code end 
上边第一个我们用的是换行符,第二个我们用的是关键字then
=end
a=if 1==1 
  5 
 end
puts a

a=if 1==1 then 5 end
puts a

a=if 1==1;5 end
puts a

#作为修饰符的if
#
code if expression
a=5 if 1==1
puts a

#unless条件式
=begin
#unless condition then code end
或者
unless expression 
  code
end
=end
unless 1==2 then
  puts "unless condition"
end

a=unless 1==2
  4
end
puts a #4

#作为修饰符的unless
#
code unless condition
a=1 unless 1==2
puts a #1

#case条件式 
x=1
name=case
  when x==1 then "one"
  when x==2 then "two"
  else "many"
  end
puts name #one

name=case
  when x==1 
    "one"
  when x==2 
    "two"
  else "many"
  end

puts name #one 

原文地址:https://www.cnblogs.com/jeriffe/p/2538378.html