PTT
Submit
Submit
選擇語言
正體中文
简体中文
PTT
Ruby
[問題] "p(true and true)" SyntaxError
作者:
putumaxally
(putumaxally)
2013-01-12 09:25:33
狀況:
想請問各位大大為什麼
p(true and true)
會顯示語法錯誤
p (true and true)
p((true and true))
就不會有錯誤
作者:
mars90226
(火星人)
2013-01-12 12:10:00
我猜測是and的執行優先順序很低,換成&&就OK
作者:
zero00072
(赤迷迭)
2013-01-12 18:09:00
and 好像被列入不建議使用的名單,就像樓上說的用 &&。
作者: bassann (Sorry or please?)
2013-01-12 19:32:00
同1樓 在這裡出錯因為程式碼想先執行其他東西: 'p(true'所以產生syntax error,而你寫的第二、三行是做一樣的事情例如 c = a and b; 效果如同 c = a; b;請參考
http://goo.gl/IDwrt
作者:
putumaxally
(putumaxally)
2013-01-12 19:57:00
那可以再問一下為什麼p(not false)也會錯誤嗎not不是control flow operator吧
作者: bassann (Sorry or please?)
2013-01-12 20:44:00
'not' still has a lower precedence(只比or/and高一點)可以參考這個表格
http://goo.gl/oayCS
ps. ! is defined as a method and not is an operator
作者:
putumaxally
(putumaxally)
2013-01-12 21:20:00
可是為什麼會發生SyntaxError呢??
作者: bassann (Sorry or please?)
2013-01-13 02:21:00
因為程式碼先evaluate其他部分 而其他部分的程式碼syntax不正確(因為切斷了) 概念有點像先乘除後加減是一樣的原因... 一個被切在'p(true'另一個是'p('not和and都是operators不是methods而operators行為像是'等於', methods像是'加減乘除'
繼續閱讀
Re: [問題] 關於IO.popen的差異
godfat
Re: [問題] 關於IO.popen的差異
kira925
[問題] 關於IO.popen的差異
kira925
[閒聊] RoR 0day SQL Injection
wstd
[問題] 編寫數學問題
cutemis
[連結] Ruby 入門教學系列文章
monomorium
[問題] rails 建立 db 時發生問題..
ilay
[問題] RARRAY_LEN取得錯誤長度
AIGecko
[問題] Gem about ranking
tonytonyjan
[問題] 升級到 rails 3 的理由
isnora
Links
booklink
Contact Us: admin [ a t ] ucptt.com