<=>运算符出问题
ruby吧
全部回复
仅看楼主
level 1
nara007 楼主
这是什么情况 一直输出 a>b
2016年01月23日 16点01分 1
level 12
$ irb
>> 8 <=> 10
=> -1
>> puts 'ok' if true
ok
=> nil
>> puts 'ok' if false
=> nil
>> puts 'ok' if 1
ok
=> nil
>> puts 'ok' if 0
ok
=> nil
>> puts 'ok' if -1
ok
=> nil
是因為 -1 是 true
2016年01月23日 23点01分 2
这个我也能想明白 只要不是0 就是true, 但是当两个字符串完全一样时 还是输出 a>b 实在搞不懂哪里出问题了
2016年01月24日 00点01分
回复 nara007 :错就错在,0自动装箱之后就是一个Fixnum对象,任何对象只要不是nil就返回true。应该说是,只要不是false或nil,那就是ture。
2016年03月27日 10点03分
回复 nara007 :参看11楼
2016年03月27日 11点03分
level 1
nara007 楼主
俩字符串完全一样 返回值肯定是0 但是还是输出 a>b
2016年01月24日 00点01分 3
level 1
nara007 楼主
<=> 这个表达式和if 一起用的时候 很诡异啊 结果很让人疑惑
2016年01月24日 00点01分 4
007:4 之后不管输入什么命令,甚至直接 puts字符串,都没有输出了 这是什么情况?
2016年01月24日 00点01分
@nara007 語法有誤。參看7樓。
2016年01月28日 00点01分
level 3
难道不是么
2016年01月25日 07点01分 5
level 3
你好好理解这个符号的意思
2016年01月25日 07点01分 6
level 12
$ irb
cannot load such file -- wirble
>> puts 'ok'
ok
=> nil
>> puts 'ok' if true
ok
=> nil
>> if true; puts 'ok'
>> end
ok
=> nil
若 if 條件與敘述同一行,之間要加;字符,且後面要有end。
若敘述行在前,if條件再後,不需加;字符,而且不需要有end。
2016年01月28日 00点01分 7
level 10
if true 后面直接分号,反正我的是过不去的。反而,我的和教程语法一致,如下:if true then puts 'ok' end
2016年01月28日 01点01分 8
level 12
也請參看 Ruby Style Guide:
https://github.com/styleguide/ruby
用then雖然也行,卻不是ruby慣用的風格,
大概是習慣其他編程語言遺留的習性,
還是盡可能避免在 if 裡用 then,
Never use then for multi-line if/unless.
# bad
if some_condition then
# body omitted
end
# good
if some_condition
# body omitted
end
另外也提到:
Favor modifier if/unless usage when you have a single-line body.
# bad
if some_condition
do_something
end
# good
do_something if some_condition
盡量在 ruby 裡入境隨俗吧!
避免在之前習慣別的編程語法帶到 ruby 裡。
2016年01月29日 15点01分 9
level 10
赞成楼上的,一般情况,ruby会把if放在后面
2016年01月31日 08点01分 10
level 8
2016年03月27日 10点03分 11
这什么设备?
2016年03月28日 06点03分
@村长小舅子🙈 ipad mini4 + serverauditor
2016年03月29日 15点03分
@AryBin 谢谢
2016年03月29日 22点03分
level 2
船操作符只有三种结果,-1,0,1,这三种在ruby中都是真,只有false和nil是假
2016年03月28日 11点03分 12
1