In rails I have this:
page_classes.split(/\s/).last
Sometimes page_classes doesn't contain any whitespace and I get the error:
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.split("\s").last
Calling .last
will ALWAYS work with an array (even empty). If you have probems, it means your page_classes
object is nil or not a string that can be processed by split()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.