Reputation: 34023
I have the following array and methods:
array = ["1", 0, "a", ""]
array.reject(&:blank?).map(&:to_i).reject{|i| i == 0}
If I'd like to save changes in place utilizing the exclamation mark, e.g. .map!(&:to_i)
, should the mark be placed after every method or otherwise - where?
Upvotes: 1
Views: 411
Reputation: 19040
Exclamation marks are not modifiers.
array.reject!(&:blank?).map!(&:to_i).reject!{|i| i == 0}
However, this code is subtly wrong. From reject!
docs:
returns nil if no changes were made.
Whoops! This could break your whole chain. Instead, you're supposed to use delete_if, which always returns the array.
array.delete_if(&:blank?).map!(&:to_i).delete_if{|i| i == 0}
Yes, it's confusing it doesn't have a bang, but it does modify in-place. From the docs:
The array is changed instantly every time the block is called, not after the iteration is over.
Upvotes: 4