如何将函数仅应用于非nil元素?

时间:2022-06-09 14:27:04

I’m using Rails 4.2.7. I have this concise function that trims (strips) strings in my array

我使用Rails 4.2.7。我有一个简洁的函数,在数组中对字符串进行修剪

data_cols = data_cols.collect{|x| x.strip || x }

However if one of the elements in the array is nil, the above falls apart. How can I modify the above so that it won’t apply to nil elements?

但是,如果数组中的一个元素是nil,那么上面的内容就会失效。如何修改上面的内容,使它不适用于nil元素?

3 个解决方案

#1


4  

If you want to remove nil values from the array entirely, you can use compact:

如果要从数组中完全删除nil值,可以使用compact:

cols.compact.map { |col| col.strip } 
# or 
cols.compact.map(&:strip) 

If you want to keep all values, but only operate on the non-nil values, you can simply use a nil-guard:

如果您希望保留所有值,但只对非nil值进行操作,您可以简单地使用一个nil-guard:

# straightforward, no magic, works across rubies:
cols.map { |col| col && col.strip }

# a wee dose of magic in ruby 2.3 (safe navigation operator)
cols.map { |col| col&.strip }

Finally, if you want to be more idiomatic and ruby-esque, you can use duck-typing and let the objects sort out themselves whether or not they can be "strip'ped":

最后,如果你想变得更地道,更像ruby,你可以使用duck-typing,让对象自己分类,不管它们是否可以被“剥掉”。

cols.map { |col| col.respond_to?(:strip) ? col.strip : col }

#2


0  

you can use try

您可以使用试

data_cols = data_cols.collect{|x| x.try(:strip) }

http://apidock.com/rails/Object/try

http://apidock.com/rails/Object/try

#3


0  

Depends if you still want the nil values in your result array. If not, you can just use compact:

取决于您是否仍然希望结果数组中的nil值。如果没有,你可以使用compact:

data_cols = data_cols.compact.collect{|x| x.strip || x }

compact will remove all the nil values in the array.

compact将删除数组中的所有nil值。

If you want to keep nil values, you need to change your condition:

如果你想保留nil值,你需要改变你的条件:

data_cols = data_cols.compact.collect{|x| x ? x.strip : nil }

#1


4  

If you want to remove nil values from the array entirely, you can use compact:

如果要从数组中完全删除nil值,可以使用compact:

cols.compact.map { |col| col.strip } 
# or 
cols.compact.map(&:strip) 

If you want to keep all values, but only operate on the non-nil values, you can simply use a nil-guard:

如果您希望保留所有值,但只对非nil值进行操作,您可以简单地使用一个nil-guard:

# straightforward, no magic, works across rubies:
cols.map { |col| col && col.strip }

# a wee dose of magic in ruby 2.3 (safe navigation operator)
cols.map { |col| col&.strip }

Finally, if you want to be more idiomatic and ruby-esque, you can use duck-typing and let the objects sort out themselves whether or not they can be "strip'ped":

最后,如果你想变得更地道,更像ruby,你可以使用duck-typing,让对象自己分类,不管它们是否可以被“剥掉”。

cols.map { |col| col.respond_to?(:strip) ? col.strip : col }

#2


0  

you can use try

您可以使用试

data_cols = data_cols.collect{|x| x.try(:strip) }

http://apidock.com/rails/Object/try

http://apidock.com/rails/Object/try

#3


0  

Depends if you still want the nil values in your result array. If not, you can just use compact:

取决于您是否仍然希望结果数组中的nil值。如果没有,你可以使用compact:

data_cols = data_cols.compact.collect{|x| x.strip || x }

compact will remove all the nil values in the array.

compact将删除数组中的所有nil值。

If you want to keep nil values, you need to change your condition:

如果你想保留nil值,你需要改变你的条件:

data_cols = data_cols.compact.collect{|x| x ? x.strip : nil }