iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 21
2
Modern Web

30天修煉Ruby面試精選30題系列 第 21

Day21 - Ruby的布林值: true,false和nil

話說這個世界上有許多真假難分的東西...

假作真時真亦假,無為有處有還無——曹雪芹《紅樓夢》。

如何用Ruby程式好好判斷呢?這就是今天的主題啦!


Ruby經典面試題目 #21

Day21 解釋Ruby裡的True, False與Nil? Explain True and False vs. "Truthy" and "Falsey / Falsy" in Ruby?

老實說這題我拖了好久,因為相關的面試題目特別多,一直在構思怎麼整理、寫成文章會比較有架構,也希望自己的產出作品也可以讓其他Ruby新手同學們比較容易了解,所以...不知不覺就到了最後10天了~~(遮臉)

先講結論:

  • (1) true物件,代表為。True is truthy.
  • (2) false物件,代表為。False is falsey.
  • (3) nil物件代表沒有的意思。 Nil is falsey. The object Nil represents “nothing”.

來列張表格:

超級比一比 意思 舉例 Truthy / Falsey
true物件 0, 1, "",[]陣列, {}區塊, [1,2,3] Truthy
false物件 1+1==3 Falsey
nil物件 沒有 nil, () Falsey

True: truely

先用Ruby程式碼來看看 0:

if 0
  puts "0 is truthy"
else
  puts "0 is falsey"
end
# Output: 0 is truthy

想起昨天的鐵人賽,最後我們在enumeratoryield的例子中用溫度來舉例,在冬天時溫度可能為零度,甚至零下(minus degree)。

大家可以把0想成真實世界中的氣溫攝氏0度,這是的確是一個確實存在的值,所以單然是true囉!

False: falsey

再來,我們用Ruby驗證一下幼稚園學過的1+1=2

if (1+1==3)
  puts "1+1==3 is truthy"
else
  puts "1+1==3 is falsey"
end
# Output: 1+1==3 is falsey

if (1+1!=2)
  puts "1+1!=2 is truthy"
else
  puts "1+1!=2 is falsey"
end
# Output: 1+1!=2 is falsey

果然,1+1只會等於2

1+1不等於2,這句話是假的。(聽起來好像在研究哲學問題啊(?!))因此我們可以得出結論false is falsey

Nil: falsey

關於nil,我把它比喻為錢包裡沒有錢~錢包沒有錢當然是假的~~!!!是假的!!!當然要催眠自己的錢包裡很有錢(挺)

if nil
  puts "nil is truthy"
else
  puts "nil is falsey"
end
# Output: nil is falsey

真的假的? truthy_or_falsey

如果我們每次想要判斷一次某物件是否為真,都要寫一次if...else end,未免也太累了...。因此最好的方法,就是用def寫一個方法!(好像在繞口令)

def truthy_or_falsey something
#此方法會傳入something參數(省略小括號)
  if something
    puts "#{something.inspect} is truthy"
  else
    puts "#{something.inspect} is falsey"
  end
end

[true, false, nil, Object, 0, 1, 1+1==3, "", ["", "", ""],(),[],{}].each do |x|
  truthy_or_falsey(x)
end

我們把各種待測試的物件裝進Array,寫一個block跑迭代傳入參數進truthy_or_falsey方法,用.inspect method依序檢查物件的布林值:

Output:

true is truthy
false is falsey # 假
nil is falsey # 假

Object is truthy # 物件為真
0 is truthy
1 is truthy

false is falsey #1+1==3,此判斷式為假

"" is truthy #空字串仍是字串,真
["", "", ""] is truthy #空陣列仍是陣列,真

nil is falsey #():未代入任何參數的小括號nil,假

[] is truthy #Array是物件,真
{} is truthy #Block是物件,真

nil (空值) 與empty string (空字串)

nil空值可以用.to_s方法轉換成empty string ""空字串。

我們先設定xfactor為空值:

xfactor = nil

print "xfactor : "
puts xfactor =="" # => false. a is nil
print "xfactor to string : "
puts xfactor.to_s =="" # => true. empty string

Output:

xfactor : false
xfactor to string : true

nil轉成字串之後,就變成物件了,所以為真:)

安能辨我是真假 - 布林值結合if|| (or,或)、 && (and, 且)

如果有兩條運算式,一條是真的,一條是假的,再用|| or連起來,結果仍為真。

|| or連起來,共存,取其一為=> 結果為(寬鬆,只要有任何為,就通過)

if (1+1==2) || (1+1!=2)
  puts "it is truthy"
else
  puts "it is falsey"
end #it is truthy! One of it is true.

反之,用&& and全部連起來,取其一為=> 結果為(嚴格,只要有任何的存在,就不通過)

=> 結果為

if (1+1==3) && (1+1!=2)
  puts "it is truthy"
else
  puts "it is falsey"
end #it is falsey! Both are false.

=> 結果為

if (1+1==2) && (1+1!=2)
  puts "it is truthy"
else
  puts "it is falsey"
end #it is falsey! One of it is false.

=> 結果為

if (1+1==2) && (2-1!=2)
  puts "it is truthy"
else
  puts "it is truthy"
end #it is truthy! Both are true.

最後,來列兩個或且九宮格比較一下:

[or . || . ]
|| | true | false
------------- | -------------|-------------|
true| true | true|
false| true | false

[and . && . ]

&& true false
true true false
false false false

我相信實務上有更多複雜的真假判斷句子,不過不用擔心!只要我們在每一天都能穩健地做好基礎打底工作,未來就不怕以假亂真了!:P

Ref:


上一篇
Day20 - 功能多多的Enumerable列舉: 移動,排序,轉換
下一篇
Day22 - Ruby比一比: and, or 和 &&, || 比較
系列文
30天修煉Ruby面試精選30題31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

1
神Q超人
iT邦研究生 5 級 ‧ 2018-10-24 09:02:37

最後這邊的例子是不是怪怪的/images/emoticon/emoticon19.gif

if (1+1==2) || (1+1!=2)
  puts "it is truthy"
else
  puts "it is falsey"
end #it is falsey! Both are false.

另外恭喜大大倒數九篇!

真是火眼金睛!貼程式碼時忘了修改成&&
大感謝幫忙除錯~~已更正:D

if (1+1==2) && (1+1!=2)
  puts "it is truthy"
else
  puts "it is falsey"
end #it is falsey! Both are false.

然後我改了文字敘述:

反之,用&& and全部連起來,取其一為=> 結果為(嚴格,只要有任何的存在,就不通過)

我要留言

立即登入留言