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