Ruby-條件判斷

=begin
      條件判斷語句包括if語句,unless語句,case語句,
=end

#if 語句
=begin
if 條件 then
  處理1
elsif 條件2 then
  處理2
else
  處理3
end
=end
a = 10
b = 20
if a > b
  puts "a bigger than b"
elsif a < b
  puts "a smaller than b"
else
  puts "a is equal to b"
end
puts "a bigger than b" if a > b

#unless 語句,與if語句相反,條件判斷為假的時(shí)執(zhí)行處理
=begin
unless 條件
  處理1
else
  處理2
end
=end
a = 10
b = 20
unless a > b
  puts "a smaller/same as than b"
end

#case 語句,適用于比較的對象只有一個(gè)的時(shí)候,根據(jù)這個(gè)對象的值不同,執(zhí)行不同的處理;when可以一次指定多個(gè)值
=begin
case 比較對象
when 值1 then
  處理1
when 值2 then
  處理2
when 值3 then
  處理3
else
  處理4
end
=end
tags = ["A", "IMG", "PRE"]
tags.each do |word|
  case word
  when "P", "A", "I", "B", "BLOCKQUOTE"
    puts "#{word} has child."
  when "IMG", "BR"
    puts "#{word} has child."
  else
    puts "#{word} cannoot be used"
  end
end

array = ["a", 1, nil]
array.each do |word|
  case word
  when String
    puts "the word is a String"
  when Numeric 
    puts "the word is a Numeric"
  else
    puts "other words are Something"
  end
end

text.each_line do |line|
  case line
  when /^From:/i
    puts "find a sender"
  when /^To:/i
    puts "find a receiver"
  when /^$/
    puts "resolution are finnished"
    exit
  else
    break
  end
end

本文題目:Ruby-條件判斷
網(wǎng)站鏈接:http://www.muchs.cn/article16/gjgjdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、商城網(wǎng)站服務(wù)器托管、App設(shè)計(jì)、網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計(jì)