[英]How can I get last only if more than one?
In rails I have this: 在铁轨上我有这个:
page_classes.split(/\s/).last
Sometimes page_classes doesn't contain any whitespace and I get the error: 有时page_classes不包含任何空格,并且出现错误:
undefined method `last' for nil:NilClass
How can I get the last unless there is only one? 除非只有一个,我如何才能获得最后一个?
尝试这个
page_classes.split(/\s/).try(:last)
The problem is about your page_classes object. 问题与您的page_classes对象有关。
page_classes.split("\s").last
Calling .last
will ALWAYS work with an array (even empty). 调用.last
将始终与数组(即使为空)一起使用。 If you have probems, it means your page_classes
object is nil or not a string that can be processed by split()
. 如果有探针,则表示page_classes
对象为nil或不是split()
可以处理的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.