使用 Ruby 1.9。我有以下内容:
add1 = [record['a'], record['b'], record['c']]
full_add = add1.reject!(&:empty?).join(", ")
# scenario 1
record['a'] = 'Good'
record['b'] = ''
record['c'] = 'Fine'
# >> Good, Fine
# scenario 2
record['a'] = 'Good'
record['b'] = 'Boy'
record['c'] = 'Fine'
# >> undefined method `join' for nil:NilClass (NoMethodError)
当我检查full_add
场景 2 时,它返回错误,因为没有数组为空。我应该如何解决这个问题?谢谢。