[問題] "p(true and true)" SyntaxError

看板Ruby作者 (Puma)時間11年前 (2013/01/12 09:25), 編輯推噓2(2015)
留言17則, 4人參與, 最新討論串1/1
狀況: 想請問各位大大為什麼 p(true and true) 會顯示語法錯誤 p (true and true) p((true and true)) 就不會有錯誤 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.118.230.226

01/12 12:10, , 1F
我猜測是and的執行優先順序很低,換成&&就OK
01/12 12:10, 1F

01/12 18:09, , 2F
and 好像被列入不建議使用的名單,就像樓上說的用 &&。
01/12 18:09, 2F

01/12 19:32, , 3F
同1樓 在這裡出錯因為程式碼想先執行其他東西: 'p(true'
01/12 19:32, 3F

01/12 19:33, , 4F
所以產生syntax error,而你寫的第二、三行是做一樣的事情
01/12 19:33, 4F

01/12 19:35, , 5F
例如 c = a and b; 效果如同 c = a; b;
01/12 19:35, 5F

01/12 19:35, , 6F
01/12 19:35, 6F

01/12 19:57, , 7F
那可以再問一下為什麼p(not false)也會錯誤嗎
01/12 19:57, 7F

01/12 20:00, , 8F
not不是control flow operator吧
01/12 20:00, 8F

01/12 20:44, , 9F
'not' still has a lower precedence(只比or/and高一點)
01/12 20:44, 9F

01/12 20:46, , 10F
可以參考這個表格 http://goo.gl/oayCS
01/12 20:46, 10F

01/12 20:48, , 11F
ps. ! is defined as a method and not is an operator
01/12 20:48, 11F

01/12 21:20, , 12F
可是為什麼會發生SyntaxError呢??
01/12 21:20, 12F

01/13 02:21, , 13F
因為程式碼先evaluate其他部分 而其他部分的程式碼syntax
01/13 02:21, 13F

01/13 02:22, , 14F
不正確(因為切斷了) 概念有點像先乘除後加減
01/13 02:22, 14F
p(true and true) 我比較能理解為什麼錯誤 可是 p(not false) 有點難懂... ※ 編輯: putumaxally 來自: 140.118.230.226 (01/13 11:18) ※ 編輯: putumaxally 來自: 140.118.230.226 (01/13 11:19)

01/13 16:41, , 15F
是一樣的原因... 一個被切在'p(true'另一個是'p('
01/13 16:41, 15F

01/13 16:42, , 16F
not和and都是operators不是methods
01/13 16:42, 16F

01/13 16:46, , 17F
而operators行為像是'等於', methods像是'加減乘除'
01/13 16:46, 17F
恩恩,懂了,感謝大大。 ※ 編輯: putumaxally 來自: 140.118.230.226 (01/13 17:27)
文章代碼(AID): #1GyBkFpt (Ruby)