【发布时间】:2018-06-17 20:23:48
【问题描述】:
我有一些代码。如果存在,它应该从字符串中删除尾随 +:
def remove_prefix(number)
number.start_with? '+' ? number[1..-1] : number
end
但它并没有按预期工作——它只是返回false:
remove_prefix('123') #=> false
remove_prefix('+123') #=> false
Rubocop 显示此错误:
Lint/LiteralAsCondition:文字“+”作为条件出现。
我做错了什么?
【问题讨论】:
-
你测试代码了吗?
-
是的......我的方法返回布尔值,但我期望字符串值。
-
仅供参考:
String#delete_prefix